1、在备用服务器上安装postgres数据库,不初始化。安装过程详见:http://www.cnblogs.com/ilifeilong/p/6979288.html2。在主服务器postgres=#CREATEROLEreplWITHREPLICATIONPASSWORD'repl'LOGIN;3上创建一个具有REPLICATION权限的复制用户postgres。允许复制用户远程连接到主服务器$grep"^host"pg_hba.confhostallall127.0.0.1/32trusthostreplicationrepl0.0.0.0/0md5hostallall::1/128trust4。设置主服务器相关的流复制参数$mkdir/usr/local/pgsql/arch$egrep"archive_mode|max_wal_senders|wal_keep_segments|archive_command|wal_level|hot_standby"postgresql.confal_level=hot_standby#minimal,archive,hot_standby,orlogicalarchive_mode=on#enablesarchivingal_comd;off,on,on,'测试!-f/usr/local/pgsql/arch/%f&&cp%p/usr/local/pgsql/arch/%f'max_wal_senders=5#maxnumberofwalsenderprocesseswal_keep_segments=30#inlogfilesegments,16MBeach;0disableshot_standby=on#"on"允许在恢复期间查询#hot_standby_feedback=off#sendinfofromstandbytoprevent5。重启主服务器进程$pg_ctlstop-mfast$pg_ctlstart6。通过pg_(start|stop)_backup函数postgres=#SELECTpg_start_backup('label',true);pg_start_backup------对主服务器进行全量备份,并传输到备服务器备份主服务器-----------7/E6000060(1row)$rsync-az--progress${PGDATA}postgres@10.189.100.195:/usr/local/pgsql/--excludepostmaster.pidpostgres=#SELECTpg_stop_backup();NOTICE:pg_stop_backupcomplete,allrequiredWALsegmentshavebeenarchivedpg_stop_backup----------------7/E60005C8(1row)在备服务器上通过pg_basebackup命令备份,要求备PGDATA目录为空$pg_basebackup--主机=10.189。102.118--username=repl--port=5432--label=backup--verbose--progress--pgdata=/usr/local/pgsql/data--checkpoint=fast--format=p--xlog-method=streamPassword:transactionlogstartpoint:7/EA000028ontimeline1pg_basebackup:startingbackgroundWALreceiver65933562/65933562kB(100%),1/1tablespacetransactionlogendpoint:7/EA000830pg_basebackup:waitingforbackgroundprocesstofinishstreaming...pg_basebackup:basebackupcompleted7。设置standby数据库恢复相关数据,使standby失效转移后可以作为主库工作$mkdir/usr/local/pgsql/arch$egrep"archive_mode|max_wal_senders|wal_keep_segments|archive_command|wal_level|hot_standby"postgresql.confwal_level=hot_standby#minimal,archive,hot_standby,orlogicalarchive_mode=on#enablesarchiving;off,on,oralwaysarchive_command='test!-f/usr/local/pgsql/arch/%f&&cp%p/usr/local/pgsql/arch/%f'max_wal_senders=5#maxnumberofwalsenderprocesseswal_keep_segments=30#inlogfilesegments,16MBach;0disableshot_standby=on#"on“允许在恢复期间查询#hot_standby_feedback=off#sendinfofromstandbytoprevent8。在standby文件创建修复文件$catrecovery.confrestore_command='cp/usr/local/pgsql/arch/%f"%p"'standby_mode='on'primary_conninfo='user=replpassword=replhost=10.189.102.118port=5432sslmode=disablesslcompression=1'archive_cleanup_command='pg_archivecleanup-d/usr/local/pgsql/arch%r>>/usr/local/pgsql/arch/archive_cleanup.log'trigger_file=‘/usr/local/pgsql/data/trigger_active_standby'9。启动待机数据库进程,自动启动流程恢复$pg_ctlstart-wwaitingforservertostart....日志:无法创建IPv6套接字:地址族不支持协议日志:重定向日志输出到日志收集器进程提示:未来日志输出将出现在目录“pg_log”中。完成服务器启动10。检查primary和备库的延迟是通过函数和系统表查看的edbstore=#select*frompg_stat_replication;#在primary主库中查看-[RECORD1]----+----------------------------pid|15013usesysid|19206usename|repapplication_name|walreceiverclient_addr|10.189.100.195client_hostname|client_port|56072backend_start|2017-06-1308:10:35.400508-07backend_xmin|state|streamingsentA|578/EClocation_location1|7/EC01A588flush_location|7/EC01A588replay_location|7/EC01A588sync_priority|0sync_state|asyncedbstore=#SELECTpg_current_xlog_location();#查看主库pg_current_xlog_location-----------------------7/EC01A588(1row)postgres=#selectpg_last_xlog_receive_location(),pg_last_xlog_replay_location(),pg_last_xact_replay_timestamp();#查看备库pg_last_xlog_receive_location|pg_last_xlog_replay_location|pg_last_xact_replay_timestamp--------------------------------+------------------------------+---------------------------7/EC01A588|7/EC01A588|2017-06-1308:25:20.281568-07(1row)按进程查看$ps-ef|grepsender|grep-vgrep#in查看postgres1501324883008:10?00:00:00postgres:walsenderprocessrepl10.189.100.195(56072)streaming7/EC01A668$ps-ef|grepreceiver|grep-vgrep#在待机库查看postgres1285712843008:10?00:00:08postgres:walreceiverprocess1streaming66/EC01A668$ps-ef|grepreceiver|grep-vgrep#
