环境:

主:192.168.1.11
备:192.168.1.12
软件安装
主库安装

 

主库创建流复制用户:

CREATE USER repuser
  REPLICATION 
  LOGIN
   PASSWORD 'repuser';

 

设置 pg_hba.conf,添加以下

host   replication     repuser          0.0.0.0/0                md5

 

设置 postgresql.conf,添加以下

hot_standby = on
max_wal_senders = 4
wal_level = hot_standby
archive_mode = on 
#archive_command = '/bin/date'   # (change requires restart) 不归档
archive_command ='cp -i %p /u01/pg_archivelog/%f 

 

主库重启一下:

pg_ctl stop
pg_ctl start

 

在备库上同步主库数据

[postgres@oradbca ~]$ pg_basebackup -D /u01/pgdata -Fp -Xs -v -P -h 192.168.1.11 -p 5432 -U repuser
transaction log start point: 0/1A000028 on timeline 1
pg_basebackup: starting background WAL receiver
37929/37929 kB (100%), 2/2 tablespaces                                         
transaction log end point: 0/1A0000F0
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed

 

在Slave端修改postgresql.conf文件

hot_standby = on

 

cp $PGHOME/share/recovery.conf.sample $PGDATA/recovery.conf

vi recovery.conf  --新增以下四行
standby_mode = 'on'
trigger_file = '/u01/pgdata/postgresql.trigger.file'
primary_conninfo = 'host=192.168.1.11 port=5432 user=repuser password=repuser  keepalives_idle=60 keepalives_interval=5 keepalives_count=5'
recovery_target_timeline = 'latest'

 

启动备库

pg_ctl start

 

后台日志输出内容为:

LOG:  entering standby mode
LOG:  redo starts at 0/1A000028
LOG:  consistent recovery state reached at 0/1B000000
LOG:  started streaming WAL from primary at 0/1B000000 on timeline 1

 

[postgres@oradbca ~]$ pg_controldata  |grep state
Database cluster state:               in archive recovery

说明已经搭建成功了。