主备切换,平时使用较多的两种方法,一个是命令切换法,另外一个是文件设置切换法,当然还有自主降级为备库法,以下主要介绍前面两种。
1、命令切换法
----停掉主库模拟主库宕库
pg_ctl -D pgbase/pgdata stop
----命令激活备库为主库
pg_ctl promote
----检测主备库的角色状态
pg_controldata | grep 'Database cluster state'
--主库状态:
Database cluster state: in production
--备库状态:
Database cluster state: in archive recovery
----修改原主库pg_hba.conf参数
host all all 192.168.163.176/32 trust
host replication replica 192.168.163.176/32 md5
host all all 192.168.163.175/32 trust
host replication replica 192.168.163.175/32 md5
----新备库recovery.conf文件添加参数
standby_mode = on
primary_conninfo = 'host=192.168.163.176 port=5433 user=replica password=replica' #primary user and port info
recovery_target_timeline = 'latest' ##表示从最新开始同步
----新备库(原主库)文件postgresql.conf添加参数
wal_sender_timeout = 60s
hot_standby = on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on
----启动新备库
pg_ctl -D pgbase/pgdata start
----验证主备同步情况
创建测试表插入测试数据,此处省略。
2、文件设置切换法
备库的recovery.conf文件添加trigger_file,并创建出这个trigger_standby空文件。
trigger_file = '/pgbase/pgdata/trigger_standby'
touch pgbase/pgdata/trigger_standby
----命令激活备库为主库
pg_ctl promote
----检查主备库状态
pg_controldata | grep 'Database cluster state'
--主库状态:
Database cluster state: in production
--备库状态:
Database cluster state: in archive recovery
----验证主备同步情况
创建测试表插入测试数据,此处省略。




