1.主库修改配置
postgresql.conf指定同步的节点
这里的standby server名称是从库的recovery.conf文件中指定的application_name
synchronous_standby_names ='pgsql_0_node_0' synchronous_commit = on #默认值就是on
2.从库修改配置
Recovery.conf
standby_mode = 'on' primary_conninfo = 'application_name=pgsql_0_node_0 host=IP_PRIMARY_NODE port=5432 user=replication_user password=PASSWORD' recovery_target_timeline = 'latest' trigger_file = '/tmp/failover_5432.trigger'
3.重启主库和从库
$ service postgresql-11 restart
4.查看状态
SELECT pid,usename,application_name,state,sync_state FROM pg_stat_replication;
至此,就从异步模式切换到同步模式了。
5.切换回到异步模式
如果想切换回到异步模式。只需要修改主库的配置
Postgresql.conf
#synchronous_standby_names = 'pgsql_0_node_0' #synchronous_commit = on
重启主库:
$ service postgresql-11 restart postgres=# SELECT pid,usename,application_name,state,sync_state FROM pg_stat_replication;