系统环境:
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*
#yum install docbook-dtds docbook-style-xsl fop libxslt -y
此步骤可选,可根据需要选择,本人是先直接编译安装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
chown -R postgres:dba /opt/data5555
chown -R postgres:dba /opt/wal5555
chown -R postgres:dba /opt/archive555
数据目录等带上端口号也是比较推荐的,尤其当存在多个实例时。
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. 初始化数据目录
chmod a+x /opt/
$ /soft/opt/pg12 /bin/initdb -D /opt/data5555 -X /opt/wal5555 -E UTF8 -U postgres -W
下面的例子使用简体中文编码
$ /soft/opt/pg12 /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='/tmp'
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 -ef | grep postgres
root 1284 1143 0 15:10 pts/0 00:00:00 su - postgres
postgres 1285 1284 0 15:10 pts/0 00:00:00 -bash
postgres 1395 1 0 15:30 ? 00:00:00 /soft/opt/pg12/bin/postgres -D /opt/data5555
postgres 1396 1395 0 15:30 ? 00:00:00 postgres: logger
postgres 1398 1395 0 15:30 ? 00:00:00 postgres: checkpointer
postgres 1399 1395 0 15:30 ? 00:00:00 postgres: background writer
postgres 1400 1395 0 15:30 ? 00:00:00 postgres: walwriter
postgres 1401 1395 0 15:30 ? 00:00:00 postgres: autovacuum launcher
postgres 1402 1395 0 15:30 ? 00:00:00 postgres: archiver
postgres 1403 1395 0 15:30 ? 00:00:00 postgres: stats collector
postgres 1404 1395 0 15:30 ? 00:00:00 postgres: logical replication launcher
postgres 1405 1285 0 15:30 pts/0 00:00:00 ps -ef
postgres 1406 1285 0 15:30 pts/0 00:00:00 grep --color=auto postgres