本文给大家推荐一个linux下自动安装postgresql15的脚本,希望能给大家带来帮助。
#!/bin/bash
set -x
#定义变量
base_dir="/opt/apps"
user_name="postgresql"
port="5432"
appuser_name="appuser"
appuser_password="appuser@123!"
appdb_name="appdb"
appuser_password_zhuanyi="appuser%40123%21"
# 获取OS版本信息
os_version=$(cat /etc/redhat-release | grep -oP '\d+' | head -1)
#获取Ip
host_ip=$(python -c "import socket;print([(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])")
#操作系统初始化
function os_init(){
# 关闭 firewalld
systemctl stop firewalld
# 禁止开机自启动
systemctl disable firewalld
# 获取当前 SELinux 状态
selinux_status=$(getenforce)
# 判断 SELinux 状态并执行相应操作
if [ "$selinux_status" == "Disabled" ]; then
echo "当前 SELinux 状态为 disabled"
elif [ "$selinux_status" == "Enforcing" ]; then
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
echo "SELinux 已禁用并配置为 disabled"
elif [ "$selinux_status" == "Permissive" ]; then
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
echo "SELinux 已配置为 disabled"
else
echo "未知的 SELinux 状态: $selinux_status"
fi
# 检查 limits.conf 文件是否包含所需配置
if grep -q "^\*\s*soft\s*nofile\s*65536" /etc/security/limits.conf && \
grep -q "^\*\s*hard\s*nofile\s*65536" /etc/security/limits.conf && \
grep -q "^\*\s*soft\s*nproc\s*65536" /etc/security/limits.conf && \
grep -q "^\*\s*hard\s*nproc\s*65536" /etc/security/limits.conf; then
echo "文件描述符限制已优化"
else
# 执行配置命令
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
echo "* soft nproc 65536" >> /etc/security/limits.conf
echo "* hard nproc 65536" >> /etc/security/limits.conf
echo "文件描述符限制已优化"
fi
# 检查 systemd 配置文件是否包含所需配置
if grep -q "^\s*DefaultLimitNOFILE=1000000" /etc/systemd/system.conf && \
grep -q "^\s*DefaultLimitNPROC=65535" /etc/systemd/system.conf; then
echo "Systemd 文件描述符和进程数限制已优化"
else
# 执行配置命令
cat >> /etc/systemd/system.conf << EOF
DefaultLimitNOFILE=1000000
DefaultLimitNPROC=65535
EOF
systemctl daemon-reexec
echo "Systemd 文件描述符和进程数限制已优化"
fi
#创建用户
if id "$user_name" &>/dev/null; then
echo "User $user_name already exists."
else
# 如果用户不存在,则创建用户
useradd "$user_name"
echo "User $user_name created successfully."
fi
#创建pg目录
mkdir -p $base_dir/$user_name/data
mkdir -p $base_dir/$user_name/run
mkdir -p $base_dir/$user_name/archive
mkdir -p $base_dir/$user_name/backup
#更改目录权限
chown -R $user_name:$user_name $base_dir/$user_name
}
function pg_install(){
# 判断操作系统版本号,安装postgresql15-server
if [ "$os_version" == "7" ]; then
cat /etc/redhat-release
#配置epel源
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
#增加postgresql源
cat > /etc/yum.repos.d/postgresql.repo << EOF
[postgresql]
name=postgresql
baseurl=https://download.postgresql.org/pub/repos/yum/15/redhat/rhel-7.9-x86_64/
enabled=1
gpgcheck=0
EOF
#安装依赖包
rpm -ivh https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libzstd-1.5.5-1.el7.x86_64.rpm
#安装postgresql15-server
yum install postgresql15-server postgresql15-contrib -y
elif [ "$os_version" == "9" ]; then
cat /etc/redhat-release
#增加postgresql源
cat > /etc/yum.repos.d/postgresql.repo << EOF
[postgresql]
name=http
baseurl=https://download.postgresql.org/pub/repos/yum/15/redhat/rhel-9.3-x86_64/
enabled=1
gpgcheck=0
EOF
#安装postgresql15-server
yum install postgresql15-server postgresql15-contrib -y
else
echo "This script is intended to run on CentOS 7."
fi
}
function pg_init(){
#initdb
su - $user_name -c "/usr/pgsql-15/bin/initdb --username=$user_name --encoding=UTF8 --lc-collate=C --lc-ctype=en_US.utf8 --data-checksums -D $base_dir/$user_name/data"
#配置pg_hba.conf文件
cat >> $base_dir/$user_name/data/pg_hba.conf << EOF
host replication all 0.0.0.0/0 trust
host all all 0.0.0.0/0 md5
EOF
#配置postgresql.conf
cat > $base_dir/$user_name/data/postgresql.conf << EOF
listen_addresses='0.0.0.0'
port=$port
max_connections=10000
unix_socket_directories='$base_dir/$user_name/run' #注:此处文件夹需要pguser001用户拥有权限
shared_buffers=1000MB #建议设置为内存的25%-40%
archive_mode='on'
archive_command='test ! -f $base_dir/$user_name/archive/%f && cp %p $base_dir/$user_name/archive/%f'
wal_level='replica'
hot_standby = on
wal_log_hints = on
max_wal_senders = 10
max_replication_slots = 10
restore_command='$base_dir/$user_name/archive/%f %p'
recovery_target='immediate'
EOF
}
#启动和开机自启动
function sh_init(){
#启动服务
su - $user_name -c "/usr/pgsql-15/bin/pg_ctl -D $base_dir/$user_name/data -l $base_dir/$user_name/pg.log start"
#配置开机自启动
chmod +x /etc/rc.d/rc.local
cat >> /etc/rc.d/rc.local << EOF
su - $user_name -c "/usr/pgsql-15/bin/pg_ctl -D $base_dir/$user_name/data -l $base_dir/$user_name/pg.log start"
EOF
#创建启动、停止、重启脚本
cat > $base_dir/$user_name/start.sh << EOF
#!/bin/bash
su - $user_name -c "/usr/pgsql-15/bin/pg_ctl -D $base_dir/$user_name/data -l $base_dir/$user_name/pg.log start"
EOF
cat > $base_dir/$user_name/stop.sh << EOF
#!/bin/bash
su - $user_name -c "/usr/pgsql-15/bin/pg_ctl -D $base_dir/$user_name/data -l $base_dir/$user_name/pg.log stop"
EOF
cat > $base_dir/$user_name/restart.sh << EOF
#!/bin/bash
su - $user_name -c "/usr/pgsql-15/bin/pg_ctl -D $base_dir/$user_name/data -l $base_dir/$user_name/pg.log restart"
EOF
#创建备份脚本
cat > $base_dir/$user_name/backup.sh << EOF
#!/bin/bash
set -ex
cmd="/usr/bin/pg_dump -Fc -v --dbname=postgresql://$appuser_name:$appuser_password_zhuanyi@$host_ip:$port/$appdb_name -f $base_dir/$user_name/backup/$appdb_name-`date +%Y-%m-%d`.dmp"
\$cmd
find $backup_dir -name "*.dmp" -mtime +30 -exec rm -f {} \;
EOF
#添加定时任务脚本
cat > $base_dir/$user_name/crontab.sh << EOF
#!/bin/bash
# 检查是否存在 crontab,如果不存在,创建一个空的 crontab
if [ -z "$(crontab -l)" ]; then
echo "" | crontab -
fi
(crontab -l ; echo "0 23 * * * sh $base_dir/$user_name/backup.sh > $base_dir/$user_name/backup.log 2>&1")|crontab -
EOF
chmod +x $base_dir/$user_name/crontab.sh
/bin/sh $base_dir/$user_name/crontab.sh
#创建恢复脚本
cat > $base_dir/$user_name/pg_restore.sh << EOF
#!/bin/bash
pg_restore -v --dbname=postgresql://$appuser_name:$appuser_password_zhuanyi@$host_ip:$port/$appdb_name $base_dir/$user_name/backup/$appdb_name.dmp
EOF
}
function db_init(){
#创建init.sql文件
cat > $base_dir/$user_name/init.sql << EOF
-- 创建数据库
CREATE DATABASE $appdb_name;
-- 连接到新创建的数据库
\c $appdb_name;
-- 创建用户
CREATE USER $appuser_name WITH PASSWORD '$appuser_password';
-- 赋予用户所有权限
GRANT ALL PRIVILEGES ON DATABASE $appdb_name TO $appuser_name;
-- 将 appdb 的所有权(OWNER)设置为 $appuser_name
ALTER DATABASE $appdb_name OWNER TO $appuser_name;
EOF
#创建drop.sql
cat > $base_dir/$user_name/drop.sql << EOF
drop database $appdb_name;
drop user $appuser_name;
EOF
#执行sql
/usr/pgsql-15/bin/psql -h $base_dir/$user_name/run -p $port -U $user_name -d postgres -f $base_dir/$user_name/init.sql
}
#初始化方法
function init(){
os_init;
pg_install;
pg_init;
sh_init;
db_init;
}
init