[root@iZ28lcmnq9qZ /root]#su postgres
[postgres@iZ28lcmnq9qZ /home/postgresql]$/usr/local/pgsql/bin/psql
postgres=# CREATE ROLE replica login replication encrypted password 'replica的密码';
# 追加
# replica为postgres用户
# XX.XX.XX.XX为从节点的完整IP,如果为网段配置就不是32了,md5为允许密码验证,trust为免密
# TYPE DATABASE USER ADDRESS METHOD
host replication replica XX.XX.XX.XX/32 trust
# 需要远程访问,记得加(还有一个监听,在后面配置)
# 0.0.0.0/0为任意IP
host all all 0.0.0.0/0 md5
# 新增或修改下列属性设置
listen_addresses = '*' # 监听所有IP
archive_mode = on # 开启归档
archive_command = 'test ! -f /usr/local/pgsql/pg_archive/%f && cp %p /usr/local/pgsql/pg_archive/%f'
wal_level = replica # 9.6开始木有hot_standby
wal_keep_segments = 16 # 暂时还不知道设置为多大合适
wal_sender_timeout = 60s
max_connections = 100 # 最大连接数,据说从机需要大于或等于该值
# 保存退出
# 建立归档文件夹
mkdir /usr/local/pgsql/pg_archive
chown -R postgres:postgres /usr/local/pgsql/pg_archive
# /usr/local/pgsql/data 为postgres的数据文件夹
pg_ctl -D /usr/local/pgsql/data -l logfile restart
# 在从节点(10.30.176.213)执行
psql -h 10.29.81.105 -U postgres
#修改postgres用户密码(数据库)
ALTER USER postgres WITH PASSWORD '你的密码';
# /usr/local/pgsql/data 为从节点的数据文件加
pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/log/pg_server.log stop
rm -rf /usr/local/pgsql/data/*
# 使用replica用户,从主节点获取备份数据(这条命令建议照抄,有兴趣在深究内部参数)
pg_basebackup -h 主节点IP -p 5432 -U replica -Fp -Xs -Pv -R -D /usr/local/pgsql/data
chown -R postgres:postgres /usr/local/pgsql/data
# 添加
standby_mode = 'on'
# 保存,退出
primary_conninfo = 'host=主节点IP port=5432 user=replica password=replica用户的密码'
recovery_target_timeline = latest #默认
max_connections = 120 #大于主节点,正式环境应当重新考虑此值的大小
hot_standby = on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on
pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/log/pg_server.log start