源码编译安装PostgreSQL(pg12)
摘要:源码安装PostgreSQL
系统环境:
Centos 7.7
PostgreSQL12.2
1. 安装依赖包
# yum -y install readline readline-devel zlib zlib-devel pam pam-devel libxml2 libxml2-devel libxslt libxslt-devel perl perl-devel tcl-devel uuid-devel gcc gcc-c++ make flex bison perl-ExtUtils*
此步骤可选,可根据需要选择,本人是先直接编译安装postgres,然后根据提示再回头安装所需的依赖包,也推荐根据报错信息来安装依赖包,加深pg所需依赖包的作用。
2. 内核参数调整
根据需要指定,可跳过,采用默认值
# vi /etc/sysctl.conf
查看已完成配置
# sysctl -p
3. 用户limits设置
建议设置一下
# vi /etc/security/limits.conf
postgres soft nofile 1048576
postgres hard nofile 1048576
postgres soft nproc 131072
postgres hard nproc 131072
postgres soft stack 10240
postgres hard stack 32768
postgres soft core 6291456
postgres hard core 6291456
4. 创建用户和组
# groupadd dba -g 2000
# useradd postgres -g 2000 -u 2000
# id postgres
uid=2000(postgres) gid=2000(dba) groups=2000(dba)
建议固化uid与gid,特别是后期配置流复制或集群。
5. 目录规划
源文件目录
# mkdir /soft
安装文件目录
# mkdir -p /soft/opt/pg12
数据目录
# mkdir -p /opt/data5555
WAL目录
# mkdir -p /opt/wal5555
归档目录
# mkdir -p /opt/archive5555
chmod -R 0770 /opt
chmod -R 0770 /soft
chown -R postgres:dba /soft
数据目录等带上端口号也是比较推荐的,尤其当存在多个实例时。
6. 编译安装
下载文件并解压
# su - postgres
$ cd /soft
$ wget https://ftp.postgresql.org/pub/source/v12.1/postgresql-12.2.tar.gz
$ tar -zxvf postgresql-12.2.tar.gz
编译
$ cd postgresql-12.2/
$ ./configure --prefix=/soft/opt/pg12 --with-pgport=5555
建议修改一下端口,不采用默认5432端口
安装
使用gmake 或者gmake world
$ gmake world
当看到最后一行显示为:
PostgreSQL, contrib, and documentation successfully made. Ready to install.
说明已经编译成功
使用gmake install或者gmake install-world 进行安装
$ gmake install-world //包含扩展包和文档
当看到最后一行显示为:
PostgreSQL, contrib, and documentation installation complete.
说明已经安装成功
查看版本
$ /soft/opt/pg12/bin/postgres --version
postgres (PostgreSQL) 12.2
7. 设置软链接
$ cd /soft/opt
$ ln -s /soft/opt/pg12 /opt/pgsql
创建一个软链接指向当前版本,当进行版本变更后,不需要调整调用脚本,只需要修改这个软链接即可,后面都会使用这个软链接。
8. 初始化数据目录
$ /opt/pgsql/bin/initdb -D /opt/data5555 -X /opt/wal5555 -E UTF8 -U postgres -W
下面的例子使用简体中文编码
$ /opt/pgsql/bin/initdb -D/opt/data5555
-E EUC_CN
--locale=zh_CN
-W
9. 修改数据库参数
下面是一个参考配置
$ vi /opt/data5555/postgresql.conf
listen_addresses=‘192.168.220.150‘
port=5555
max_connections=1000
unix_socket_directories=‘/opt/data5555‘
wal_level=logical
archive_mode=on
archive_command=‘cp %p /opt/archive5555/%f‘
max_wal_size=1GB
max_wal_senders=10
wal_keep_segments=512
hot_standby=on
logging_collector=on
log_destination=csvlog
log_directory=‘/opt/data5555/pg_log‘
log_filename=‘pg_log_%Y-%m-%d_%H%M%S.log‘
log_file_mode=0600
log_truncate_on_rotation=on
log_rotation_age=1d
log_rotation_size=10MB
log_min_messages=warning
log_min_duration_statement=30s
log_checkpoints=on
log_connections=on
log_disconnections=on
log_duration=on
log_lock_waits=on
log_statement=DDL
10.配置环境变量
$ vi ~/.bash_profile
export PGPORT=5555
export PGUSER=postgres
export PGHOME=/opt/pgsql
export PGDATA=/opt/data5555
export PATH=$PGHOME/bin:$PATH
11.手工启停数据库
查看数据库运行状态
$ pg_ctl -D /opt/data5555 status
启动数据库
$ pg_ctl -D /opt/data5555 start &
停止数据库
$ pg_ctl -D /opt/data5555 stop
12.查看实例进程
$ ps f -u postgres
PID TTY STAT TIME COMMAND
1323 pts/0 S 0:00 -bash
1456 pts/0 R+ 0:00 \_ ps f -u postgres
1058 ? Ss 0:00 /opt/pg12/bin/postgres -D /opt/data5555
1068 ? Ss 0:00 \_ postgres: logger
1070 ? Ss 0:00 \_ postgres: checkpointer
1071 ? Ss 0:00 \_ postgres: background writer
1072 ? Ss 0:00 \_ postgres: walwriter
1073 ? Ss 0:00 \_ postgres: autovacuum launcher
1074 ? Ss 0:00 \_ postgres: archiver last was 000000010000000000000008
1075 ? Ss 0:00 \_ postgres: stats collector
1076 ? Ss 0:00 \_ postgres: logical replication launcher
13.开机自启动
root用户配置
# cp /soft/postgresql-12.2/contrib/start-scripts/linux /etc/init.d/postgres-12
# chmod +x /etc/init.d/postgres-12
# chkconfig postgres-12 on
# vi /etc/init.d/postgres-12
prefix=/opt/pgsql
PGDATA="/opt/data5555"
PGUSER=postgres
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PGCTL="$prefix/bin/pg_ctl"
set -e
# Only start if we can find the postmaster.
test -x $DAEMON ||
{
echo "$DAEMON not found"
if [ "$1" = "stop" ]
then exit 0
else exit 5
fi
}
# Parse command line parameters.
case $1 in
start)
echo -n "Starting PostgreSQL: "
su - $PGUSER -c "$PGCTL -D ‘$PGDATA‘ start >>/dev/null &"
echo "ok"
;;
stop)
echo -n "Stopping PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D ‘$PGDATA‘ -s"
echo "ok"
;;
restart)
echo -n "Restarting PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D ‘$PGDATA‘ -s"
su - $PGUSER -c "$PGCTL -D ‘$PGDATA‘ start >>/dev/null &"
echo "ok"
;;
reload)
echo -n "Reload PostgreSQL: "
su - $PGUSER -c "$PGCTL reload -D ‘$PGDATA‘ -s"
echo "ok"
;;
status)
su - $PGUSER -c "$PGCTL status -D ‘$PGDATA‘"
;;
*)
# Print help
echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
exit 1
;;
Esac
exit 0