​​​​Linux Shell 实现一键部署postgres15

news/2024/7/3 4:39:08

postgres 前言

PostgreSQL 是一个功能强大的开源对象关系数据库系统,拥有超过 35 年的积极开发经验 这为其赢得了可靠性、功能稳健性和性能的良好声誉。

通过官方文档可以找到大量描述如何安装和使用 PostgreSQL 的信息。 开源社区提供了许多有用的地方来熟悉PostgreSQL, 了解其运作方式,并寻找职业机会。了解更多有关 如何与社区互动。

 download postgres

postgres allpgadmin all文档
downloaddownload参考

Linux 各系统下载使用参考

Red HatRocky Linux Oracle Linux

AlmaLinux 

ubuntususelinuxesxiRHEL标准安装系统安装参考YUM参考

MobaXterm 远程连接工具

Red Hat Enterprise 9.0 文档Kickstart 生成器
downloaddownloaddownloaddownloaddownloaddownloaddownload参考参考配置参考download参考Kickstart 
版本兼容性

postgres 一键自动化部署

  • 最终实现在线下载postgres,编译安装postgres,环境变量,初始化postgres数据库,用户密码配置,用户权限设置,远程连接设置,数据库创建postdb,启动脚本创建,防火墙配置,安装包删除。
  • postgres/Report@123 #postgres用户名密码
  • postgres / postgres #用户名,组
  • /opt/pgsql/data #数据目录
  • /opt/pgsql #安装目录
  • postgresql 5432 #postgresql端口
  • /opt/pgsql/data/pg_hba.conf #远程连接授权
  • /opt/pgsql/data/postgresql.conf #远程连接授权
  • /usr/lib/systemd/system/postgresql.service #postgresql启动服务脚本
  • su  - postgres -c 'psql -c "SELECT version();"' #postgres版本获取
  • if 判断是否存在postgres用户(if ! compgen -u postgres  &> /dev/null)
  • increase indent:Tab
  • decrease indent:Shift+Tab
vim /postgres_15_install.sh
#!/bin/bash
# -*- coding: utf-8 -*-
# Author: CIASM
# update 2023/06/02
# increase indent:Tab
# decrease indent:Shift+Tab
# install source postgres

install_postgres (){

if ! compgen -u postgres  &> /dev/null
    then
	
	if [ $? -eq 0 ];then
	
postgres_user=postgres
postgres_password=postgres@123

pgsql_opt=/opt
pgsql_url=https://ftp.postgresql.org/pub/source/v15.3/
pgsql_gz=postgresql-15.3.tar.gz
pgsql_data=data
pgsql_download=download
pgsql_pgsql=pgsql
pgsql_decompression_directory=postgresql-15.3
pgsql_installation_directory=/opt/pgsql

echo "Create user and groups for postgres Database service"
groupadd $postgres_user
useradd -m -g $postgres_user -d $pgsql_installation_directory $postgres_user

mkdir -p $pgsql_installation_directory/{/$pgsql_data,/$pgsql_download}
chown -R $postgres_user:$postgres_user $pgsql_installation_directory/$pgsql_data

echo "Firewall port development"
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
firewall-cmd --zone=public --add-port=5432/tcp --permanent && firewall-cmd --reload

echo "Add postgres users to users and user groups"
echo "$postgres_password" | passwd --stdin $postgres_user

echo "Dependent installation"
yum install -y http://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
yum install -y ncurses-devel readline-devel zlib zlib-devel perl-ExtUtils-Embed
yum install -y lz4-devel openssl openssl-devel libxml2 libxml2-devel pam pam-devel systemd-devel
yum install -y gcc gcc-c++ net-tools make cmake

echo "download pgsql"
wget -N -P $pgsql_opt/$pgsql_download $pgsql_url$pgsql_gz

echo "install pgsql"
tar -zxf $pgsql_opt/$pgsql_download/$pgsql_gz -C $pgsql_opt
cd $pgsql_opt/$pgsql_decompression_directory
./configure --prefix=$pgsql_installation_directory && make && make install
	
echo "pgsql environment variable"
su - postgres <<EOF
echo 'umask 022' >>  ~/.bash_profile
echo 'export PGHOME=/opt/pgsql' >>  ~/.bash_profile
echo 'export PGDATA=/opt/pgsql/data' >>  ~/.bash_profile
echo 'export PATH=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/pgsql/bin' >>  ~/.bash_profile
echo 'export MANPATH=/opt/pgsql/share/man' >>  ~/.bash_profile
echo 'export LD_LIBRARY_PATH=/opt/pgsql/lib' >>  ~/.bash_profile
echo 'export LANG=en_US.utf8' >>  ~/.bash_profile
source  ~/.bash_profile
/opt/pgsql/bin/initdb -D /opt/pgsql/data
/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile start
psql -c "ALTER USER postgres WITH PASSWORD 'Report@123'"
sed -i "59a\listen_addresses = '*'" /opt/pgsql/data/postgresql.conf
echo 'host    all             all             0.0.0.0/0             md5' >> /opt/pgsql/data/pg_hba.conf 
psql -c "SELECT version();"
psql -c "create database postdb owner postgres;"
psql -c "grant all privileges on database postdb to postgres;"
/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile reload
EOF

echo "postgresql.service add system"
cat >>/usr/lib/systemd/system/postgresql.service<<EOF
[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking
User=postgres
Group=postgres
Environment=PGHOME=/opt/pgsql
Environment=PGDATA=/opt/pgsql/data
ExecStart=/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data start
ExecStop=/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile stop
ExecReload=/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile reload
Restart=always

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl enable postgresql

echo "Deleting an installation package"
rm -rf $pgsql_opt/$pgsql_download $pgsql_opt/$pgsql_decompression_directory

	echo -e "\033[32mThe postgresql Install Success...\033[0m" 
	else
	echo -e "\033[31mThe postgresql Install Failed...\033[0m" 
	exit 1
	fi
else
	echo -e "\033[33mThe postgresql Install already...\033[0m"
fi
}

main (){
	install_postgres
}
 
main

 执行一键安装postgres

sh /postgres_15_install.sh

pgadmin连接postgres

  • 端口5432
  • 用户名密码postgres/Report@123
  • postgres IP地址

 


http://lihuaxi.xjx100.cn/news/1197895.html

相关文章

Babylon.js入门教程:探索3D世界的新维度

随着3D技术的不断发展&#xff0c;越来越多的开发者开始涉足3D领域。在这个领域中&#xff0c;Three.js和Babylon.js是最受欢迎的两个框架。本文将为大家介绍Babylon.js的入门教程&#xff0c;并与Three.js进行对比&#xff0c;探讨它们的优缺点。 一、Babylon.js简介 Babylo…

航天航空飞机火箭模型3D打印制作服务/增材制造航空模型制作

3D打印是对“增材制造”这种材料成型工艺的通俗叫法。3D打印是制造业有代表性的颠覆性技术&#xff0c;区别于传统的材料成型工艺&#xff0c;在加工的过程中材料质量不减反增&#xff0c;通过“自下而上”的材料累加来成型。 【CASAIM智能制造】是中科院下属机构&#xff0c;作…

[详细的微信授权登陆 demo]

目录 前言: Java实现微信授权登录的步骤如下&#xff1a; 生成授权链接&#xff0c;让用户点击该链接进行授权。可以使用WeixinService的getAuthorizeUrl方法来生成授权链接&#xff1a; 其中&#xff0c;redirectUrl是用户授权后跳转的链接&#xff0c;snsapi_userinfo表…

Qt中的窗口类及其特点

目录 常用的窗口类 窗口的显示内嵌窗口 QWidget内嵌窗口演示 QWidget不内嵌窗口演示 QDialog类型的窗口特点 QMainWindows窗口的特点 总结 常用的窗口类 常用的窗口类有 3 个 在创建 Qt 窗口的时候&#xff0c;需要让自己的窗口类继承上述三个窗口类的其中一个QWidget 所有…

【是德出品,必属精品】示波器探头的11个误解

误解1. 100 MHz 的“信号”&#xff0c;使用 100 MHz 的示波器探头。 示波器探头带宽与配合它们使用的示波器带宽采用相同的方法进行规定&#xff0c;即产品响应的 -3dB 点。举例来说&#xff0c;如果使用 100 MHz 带宽的探头测量 100 MHz 1Vpp 正弦波&#xff0c;那么探头输出…

STL-string-1

stoi int stoi (const string& str, size_t* idx 0, int base 10);int stoi (const wstring& str, size_t* idx 0, int base 10); Convert string to integer 解析str&#xff0c;将其内容解释为指定基数的整数&#xff0c;该整数作为int值返回。 如果idx不是空…

视频美颜sdk是什么?技术解析与实现原理详解

视频美颜技术的发展则为人们提供了一种美化自己的方式&#xff0c;因此&#xff0c;视频美颜技术成为了一个备受关注的领域。在这个领域中&#xff0c;视频美颜sdk技术则是实现高效美颜的关键因素之一。本文将从技术角度分析视频美颜sdk的实现原理和优势。 一、视频美颜技术的…

虚拟现实 VR 智慧办公室可视化

“虚拟现实”是来自英文“Virtual Reality”&#xff0c;简称 VR 技术&#xff0c;其是通过利用计算机仿真系统模拟外界环境&#xff0c;主要模拟对象有环境、技能、传感设备和感知等&#xff0c;为用户提供多信息、三维动态、交互式的仿真体验。 图扑软件基于自研可视化引擎 H…