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

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
26天前
|
Shell Linux
【linux】Shell脚本中basename和dirname的详细用法教程
本文详细介绍了Linux Shell脚本中 `basename`和 `dirname`命令的用法,包括去除路径信息、去除后缀、批量处理文件名和路径等。同时,通过文件备份和日志文件分离的实践应用,展示了这两个命令在实际脚本中的应用场景。希望本文能帮助您更好地理解和应用 `basename`和 `dirname`命令,提高Shell脚本编写的效率和灵活性。
88 32
|
23天前
|
JSON Shell 数据格式
使用 pipx 安装并执行 Python 应用程序 (1)
使用 pipx 安装并执行 Python 应用程序 (1)
75 17
|
10天前
|
JavaScript Shell C#
多种脚本批量下载 Docker 镜像:Shell、PowerShell、Node.js 和 C#
本项目提供多种脚本(Shell、PowerShell、Node.js 和 C#)用于批量下载 Docker 镜像。配置文件 `docker-images.txt` 列出需要下载的镜像及其标签。各脚本首先检查 Docker 是否安装,接着读取配置文件并逐行处理,跳过空行和注释行,提取镜像名称和标签,调用 `docker pull` 命令下载镜像,并输出下载结果。使用时需创建配置文件并运行相应脚本。C# 版本需安装 .NET 8 runtime。
77 1
|
2月前
|
IDE 测试技术 项目管理
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
PyCharm是由JetBrains开发的Python集成开发环境(IDE),专为Python开发者设计,支持Web开发、调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试和版本控制等功能。它有专业版、教育版和社区版三个版本,其中社区版免费且适合个人和小型团队使用,包含基本的Python开发功能。安装PyCharm前需先安装Python解释器,并配置环境变量。通过简单的步骤即可在PyCharm中创建并运行Python项目,如输出“Hello World”。
368 13
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
|
2月前
|
人工智能 Java Python
python安装、vscode安装、conda安装:一文搞定Python的开发环境(史上最全)
尼恩架构团队推出了一系列《LLM大模型学习圣经》PDF,旨在帮助读者深入理解并掌握大型语言模型(LLM)及其相关技术。该系列包括Python基础、Transformer架构、LangChain框架、RAG架构及LLM智能体等内容,覆盖从理论到实践的各个方面。此外,尼恩还提供了配套视频教程,计划于2025年5月前发布,助力更多人成为大模型应用架构师,冲击年薪百万目标。
|
2月前
|
安全 Linux 网络安全
利用Python脚本自动备份网络设备配置
通过本文的介绍,我们了解了如何利用Python脚本自动备份网络设备配置。该脚本使用 `paramiko`库通过SSH连接到设备,获取并保存配置文件。通过定时任务调度,可以实现定期自动备份,确保网络设备配置的安全和可用。希望这些内容能够帮助你在实际工作中实现网络设备的自动化备份。
74 14
|
2月前
|
Shell Linux iOS开发
使用 pipx 安装并执行 Python 应用程序 (1)
使用 pipx 安装并执行 Python 应用程序 (1)
91 0
使用 pipx 安装并执行 Python 应用程序 (1)
|
2月前
|
人工智能 编译器 Python
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
57 0
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
|
5月前
|
Linux 区块链 Python
Python实用记录(十三):python脚本打包exe文件并运行
这篇文章介绍了如何使用PyInstaller将Python脚本打包成可执行文件(exe),并提供了详细的步骤和注意事项。
210 1
Python实用记录(十三):python脚本打包exe文件并运行
|
6月前
|
存储 Shell 区块链
怎么把Python脚本打包成可执行程序?
该文档介绍了如何将Python脚本及其运行环境打包成EXE可执行文件,以便在不具备Python环境的计算机上运行。首先确保Python脚本能够正常运行,然后通过安装PyInstaller并使用`--onefile`参数将脚本打包成独立的EXE文件。此外,还提供了去除命令行窗口和指定可执行文件图标的详细方法。这些步骤帮助用户轻松地将Python程序分发给最终用户。
怎么把Python脚本打包成可执行程序?

热门文章

最新文章

推荐镜像

更多