暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

postgres14一键安装脚本分享(shell和python)


今天介绍两种安装postgresql的脚本。第一部分是python,第二部分是shell。

前期准备工作

安装包上传至packages目录下,脚本和packages同层级即可,本次只分享rpm包安装,二进制可以根据脚本内容进行修改。安装包链接如下:


一、python脚本

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import os

import time

def install():

    """

    postgresql14-14.2安装

    sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

    yum install -y --downloadonly --downloaddir=./packages  postgresql14-server

    --

    rpm -Uvh --force --nodeps ./packages/*.rpm

    sudo usr/pgsql-14/bin/postgresql-14-setup initdb

    sudo systemctl enable postgresql-14

    sudo systemctl start postgresql-14

    sudo  -H  -u postgres psql -c "create user ceshi with password '123456';"

    sudo  -H  -u postgres psql -c "create database test_db owner ceshi;"

    sudo  -H  -u postgres psql -c "grant all privileges on database test_db to ceshi;"

    :return:

    """

    os.system("rpm -Uvh --force --nodeps ./packages/*.rpm")

    os.system("sudo usr/pgsql-14/bin/postgresql-14-setup initdb")

    os.system("sudo systemctl enable postgresql-14")

    os.system("sudo systemctl start postgresql-14")

    os.system("sudo  -H  -u postgres psql -c \"create user ceshi with password '123456';\"")

    os.system("sudo  -H  -u postgres psql -c \"create database test_db owner ceshi;\"")

    os.system("sudo  -H  -u postgres psql -c \"grant all privileges on database test_db to ceshi;\"")

def config_postgresql():

    """

    配置postgresql14-14.2

    vi var/lib/pgsql/14/data/postgresql.conf

    #listen_addresses = 'localhost'

    listen_addresses = '*'

    vi /var/lib/pgsql/14/data/pg_hba.conf

    host    all            all            0.0.0.0/0              md5

    :return:

    """

    with open("/var/lib/pgsql/14/data/postgresql.conf", "r+") as conf_file:

        file_context = conf_file.read()

        file_context = file_context.replace("daemonize no", "daemonize yes").replace("#listen_addresses = 'localhost'",

                                                                                    "listen_addresses = '*'")

        conf_file.write(file_context)

    with open("/var/lib/pgsql/14/data/pg_hba.conf", "a") as conf_file:

        conf_file.write("host    all            all            0.0.0.0/0              md5")

    os.system("sudo systemctl restart postgresql-14")

def detect():

    """

    检测是否安装成功

    :return:

    """

    time.sleep(5)

    return True if os.popen("systemctl status postgresql-14").read().find(

        "active (running)") > 0 else False

def prompt_fail():

    """

    安装失败后提示

    :return:

    """

    print """

    \033[5;31;40m 安装失败 \033[0m

    """

def prompt_success():

    """

    安装成功后提示

    :return:

    """

    print """

    \033[5;32;40m 恭喜postgresql14-14.2安装成功!\033[0m

    使用前注意:

        测试前可以先关闭防火墙:systemctl stop firewalld

      使用sudo权限的非root用户去执行,因为root用户不允许创建数据库账号和库。

        postgresql14 以添加默认账号、开启远程连接、并设置好开机自启:

        账号:ceshi

        密码:123456

        默认数据库:test_db

        端口:5432

        配置文件路径:/var/lib/pgsql/14/data/

    停止命令:systemctl stop postgresql-14

    启动命令:systemctl start postgresql-14

    重启命令:systemctl restart postgresql-14

    """

if __name__ == '__main__':

    install()

    config_postgresql()

    if detect():

        prompt_success()

    else:

        prompt_fail()

切换目录下添加执行权限X

chmod +x install.py

一键安装

./install.py

二、shell脚本

#!/bin/bash

# 检查是否以 root 用户运行

if [ "$EUID" -ne 0 ]; then

  echo "请以非root的sudo用户运行此脚本。"

  exit 1

fi

# 安装必要的依赖

echo "安装依赖包..."

yum install -y epel-release

yum install -y wget vim

# 下载并安装 PostgreSQL 官方YUM repository 配置包

echo "添加 PostgreSQL YUM repository..."

yum install -y --downloadonly --downloaddir=./packages  postgresql14-server

# 安装 PostgreSQL

echo "安装 PostgreSQL 14"

rpm -Uvh --force --nodeps ./packages/*.rpm

# 初始化数据库集群

echo "初始化数据库..."

sudo /usr/pgsql-14/bin/postgresql-14-setup initdb

# 启动并启用 PostgreSQL 服务

echo "启动并启用 PostgreSQL 服务..."

sudo systemctl enable postgresql-14

sudo systemctl start postgresql-14

# 创建一个测试用户和数据库

echo "创建测试用户和数据库..."

sudo -u postgres psql -c "create user myuser with password 'mypassword';"

sudo -u postgres psql -c "create database mydb owner myuser;"

# 设置postgresql.conf 和 pg_hba.conf 的基本配置

echo "配置 postgresql.conf 和 pg_hba.conf..."

PG_CONF_DIR="/var/lib/pgsql/$PG_VERSION/data"

cat >> $PG_CONF_DIR/postgresql.conf <<EOL

listen_addresses = '*'

EOL

cat >> $PG_CONF_DIR/pg_hba.conf <<EOL

host    all            all            0.0.0.0/0              md5

EOL

# 重启 PostgreSQL 服务以应用配置更改

echo "重启 PostgreSQL 服务..."

systemctl restart postgresql-14

# 提示安装完成

echo "PostgreSQL $PG_VERSION 安装完成并已启动。"

echo "可以使用命令 'sudo -u postgres psql' 连接到 PostgreSQL 数据库。"

赋予install.sh执行权限

chmod +x install.sh

一键安装

sh -x install.sh



文中的概念来源于网络,如有侵权,请联系我删除。

欢迎关注公众号:小周的数据库进阶之路,一起交流数据库、中间件和云计算等技术。欢迎觉得读完本文有收获,可以转发给其他朋友,大家一起学习进步!感兴趣的朋友可以加我微信,拉您进群与业界的大佬们一起交流学习。


文章转载自小周的数据库进阶之路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论