目录
安装包下载
https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz
安装脚本
-
将脚本(postgresql-install.sh)和安装包(postgresql-15.3.tar.gz)放置/data路径
-
运行脚本sh postgresql-install.sh
-
INSTALL_DIR为安装目录,$INSTALL_DIR/data为数据存放位置
#!/bin/bash -e
PASSWORD="123456"
INSTALL_DIR="/data/postgresql-15.3"解压缩文件
tar -zxvf postgresql-15.3.tar.gz || { echo "解压文件失败"; exit 1; }
检查是否安装了readline-devel
rpm -qa | grep readline-devel || { echo "readline-devel未安装"; exit 1; }
安装readline-devel
yum install readline-devel -y || { echo "安装readline-devel失败"; exit 1; }
编译安装
cd postgresql-15.3
./configure --prefix=$INSTALL_DIR && make && make install || { echo "编译安装失败"; exit 1; }配置环境变量
echo "export PATH=INSTALL_DIR/bin:\$PATH" >> /etc/profile || { echo "配置环境变量失败"; exit 1; } echo "export LD_LIBRARY_PATH=INSTALL_DIR/lib:$LD_LIBRARY_PATH" >> /etc/profile || { echo "配置环境变量失败"; exit 1; }
source /etc/profile将postgresql注册为服务
cd INSTALL_DIR/contrib/start-scripts/ chmod a+x linux || { echo "修改权限失败"; exit 1; } cp linux /etc/init.d/postgresql || { echo "复制文件失败"; exit 1; } sed -i "s#prefix=/usr/local/pgsql#prefix=INSTALL_DIR#g" /etc/init.d/postgresql || { echo "修改文件失败"; exit 1; }
sed -i "s#PGDATA="/usr/local/pgsql/data"#PGDATA=$INSTALL_DIR/data#" /etc/init.d/postgresql || { echo "修改文件失败"; exit 1; }创建用户
if ! id -u postgres > /dev/null 2>&1; then
useradd postgres
fi创建数据目录,并赋予权限
chown -R postgres:postgres INSTALL_DIR mkdir INSTALL_DIR/data || { echo "创建数据目录失败"; exit 1; }
chown -R postgres:postgres $INSTALL_DIR/data切换用户初始化数据库
su - postgres -c "INSTALL_DIR/bin/initdb -D INSTALL_DIR/data/" || { echo "初始化数据库失败"; exit 1; }
修改配置文件
echo "host all all 0.0.0.0/0 password" >> $INSTALL_DIR/data/pg_hba.conf || { echo "修改配置文件失败"; exit 1; }
修改listen_addresses为*
sed -i "s/^#listen_addresses = .*/listen_addresses = '*'/g" INSTALL_DIR/data/postgresql.conf || { echo "修改配置文件失败"; exit 1; }
修改max_connection为1000
sed -i 's/^#max_connections = .*/max_connections = 1000/' INSTALL_DIR/data/postgresql.conf || { echo "修改配置文件失败"; exit 1; }
设置开机自启
chkconfig --add postgresql || { echo "设置开机自启失败"; exit 1; }
启动服务
service postgresql start || { echo "启动服务失败"; exit 1; }
显示状态
service postgresql status || { echo "显示状态失败"; exit 1; }
修改密码
su - postgres -c "INSTALL_DIR/bin/psql -c \"ALTER USER postgres WITH PASSWORD 'PASSWORD';"" || { echo "修改密码失败"; exit 1; }
服务常用命令
# 启动服务
service postgresql start
# 停止服务
service postgresql stop
# 显示状态
service postgresql status
# 重新加载配置(这个命令用于重新加载 PostgreSQL 的配置文件,而不需要完全重启服务。当只修改了一些可以动态加载的配置参数(如日志级别等)时,使用这个命令可以在不中断服务运行的情况下使新配置生效)
service postgresql reload