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

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介: 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

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
10天前
|
存储 Shell 区块链
怎么把Python脚本打包成可执行程序?
该文档介绍了如何将Python脚本及其运行环境打包成EXE可执行文件,以便在不具备Python环境的计算机上运行。首先确保Python脚本能够正常运行,然后通过安装PyInstaller并使用`--onefile`参数将脚本打包成独立的EXE文件。此外,还提供了去除命令行窗口和指定可执行文件图标的详细方法。这些步骤帮助用户轻松地将Python程序分发给最终用户。
怎么把Python脚本打包成可执行程序?
|
10天前
|
Shell
Shell脚本有哪些基本语法?
【9月更文挑战第4天】
35 17
|
4天前
|
安全 JavaScript 前端开发
自动化测试的魔法:如何用Python编写你的第一个测试脚本
【8月更文挑战第41天】在软件的世界里,质量是王道。而自动化测试,就像是维护这个王国的骑士,确保我们的软件产品坚不可摧。本文将引导你进入自动化测试的奇妙世界,教你如何使用Python这把强大的魔法杖,编写出能够守护你代码安全的第一道防护咒语。让我们一起开启这场魔法之旅吧!
|
10天前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
26 12
|
8天前
|
网络协议 关系型数据库 MySQL
Shell 脚本案例
Shell 脚本案例
25 8
|
9天前
|
Shell Linux 开发工具
linux shell 脚本调试技巧
【9月更文挑战第3天】在Linux中调试shell脚本可采用多种技巧:使用`-x`选项显示每行命令及变量扩展情况;通过`read`或`trap`设置断点;利用`echo`检查变量值,`set`显示所有变量;检查退出状态码 `$?` 进行错误处理;使用`bashdb`等调试工具实现更复杂调试功能。
|
10天前
|
存储 Java 开发者
python脚本实现原理
【9月更文挑战第4天】python脚本实现原理
25 5
|
7天前
|
运维 监控 API
自动化运维:使用Python脚本进行日常管理
【9月更文挑战第6天】在现代的IT环境中,自动化运维已成为提升效率、减少人为错误的关键。本文将介绍如何通过Python脚本简化日常的运维任务,包括批量配置管理和日志分析。我们将从基础语法讲起,逐步深入到脚本的实际应用,旨在为读者提供一套完整的解决方案,以实现运维工作的自动化和优化。
11 1
|
11天前
|
运维 Linux 测试技术
自动化运维:使用Python脚本简化日常任务
【8月更文挑战第34天】在快节奏的IT环境中,自动化运维成为提升效率、降低错误率的关键。本文以Python脚本为例,展示如何通过编写简单的脚本来自动化日常运维任务,如批量更改文件权限、自动备份数据等。文章不仅提供代码示例,还探讨了自动化运维带来的益处和实施时应注意的问题。