PostgreSQL 11.3一臺服務器上配置主從同步流複製筆記

Environment

The parent directory is:node

/Users/tom

two folders:sql

testdb113-->master node  
 testdb113b->slave node

Create the master

mkdir testdb113
initdb -D ./testdb113
pg_ctl -D ./testdb113 start

Create the slave

mkdir testdb113b
pg_basebackup  -D ./testdb113b
chmod -R 700 ./testdb113b

Prepare the master for hot-standby

Create the user for stream replication

CREATE ROLE replicauser WITH REPLICATION LOGIN ENCRYPTED PASSWORD 'abcdtest';

Create the archive folder

master:app

mkdir ./testdb113/archive

Edit postresql.conf

master: postgresql.confpost

wal_level = replica 
synchronous_commit = remote_apply 
archive_mode = on 
archive_command = 'cp %p /Users/tom/testdb113/archive/%f'
max_wal_senders =  2 
wal_keep_segments = 10 
synchronous_standby_names = 'pgslave001'

Edit pg_hba.conf

master: pg_hba.confrest

host     replication    replicauser          127.0.0.1/32            md5
host     replication    replicauser          ::1/128                 md5

Prepare the slave for hot-standby

Edit postgresql.conf

standby: postgresql.confpostgresql

port = 6432 
hot_standby = on

Create/edit recovery.conf

standby: recovery.confcode

standby_mode = 'on'
primary_conninfo = 'host=localhost port=5432 user=replicauser password=abcdtest application_name=pgslave001'
restore_command = 'cp /Users/tom/testdb113/archive/%f %p'
trigger_file = '/tmp/postgresql.trigger.6432'

Start and test

Restart the master:

pg_ctl -D ./testdb113 restart

Start the slave

pg_ctl -D ./testdb113b start

Change at master

create table test ( id int, name varchar(100));
insert into test values(1,'Tom');

Read at slave

select * from test;
相關文章
相關標籤/搜索