User Tools

Site Tools


postgresqlreplication

This is an old revision of the document!


Binary Replication

Settings

Description Value
Primary Server 192.168.0.2
Standby Server 192.168.0.3
Default Port 5432

Create Replication User

# su - postgres
# createuser -U postgres repmgr -P -c 5 --replication

Configure pg_hba.conf

Add a rule that will allow the database user from the standby access the primary.

# Allow replication connections
host     replication     repmgr         [standby-IP]/32        md5

Configure postgresql.conf (PRIMARY)

wal_level = replica
archive_mode = on
archive_command = 'test ! -f /var/lib/pgsql/pg_log_archive/%f && cp %p /var/lib/pgsql/pg_log_archive/%f'
max_wal_senders = 3

Stop PostgreSQL on Standby Server

pg_ctl stop
pg_ctl status

Delete Standby Database and Directory

cd /var/lib/postgresql/10
rm -rf main

Backup Primary Database from Standby Server

pg_basebackup -D <OutputDirectory> -h <HostName> -U <User> -v -P -c fast -X stream -R

-v        Verbose
-P        Progress Reporting
-c fast   Fast Checkpoint
-X stream Copy WAL files at the same time
-R        Create minimal recovery.conf file

Once the copy has completed, you will see the following message in the nohup.out file:

pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed

Configure postgresql.conf (STANDBY)

hot_standby = on

Edit recovery.conf on Standby

standby_mode = on
primary_conninfo = 'host=[Primary IP] port=5432 user=repmgr password=[Password]'

Start PostgreSQL on Standby Server

pg_ctl start
pg_ctl status

Check Replication Progress

ls -lrt $(ps -ef | grep postgres | grep recovering | grep -v grep | awk {'print $12'})

tail -f /var/log/postgresql/postgresql-9.4-main.log
WAIT FOR : consistent recovery state reached at ...
           database system is ready to accept read only connections
SELECT * FROM PG_STAT_REPLICATION;
SELECT PG_IS_IN_RECOVERY();
SELECT TXID_CURRENT_SNAPSHOT();
SELECT * FROM PG_LAST_XACT_REPLAY_TIMESTAMP();
 
-- Note some function names changed in PostgreSQL 10
SELECT pg_is_in_recovery(),
pg_is_wal_replay_paused(),
pg_last_wal_receive_lsn(),
pg_last_wal_replay_lsn(),
pg_last_xact_replay_timestamp(),
round(EXTRACT(epoch FROM now() - pg_last_xact_replay_timestamp())) AS slave_lag;

Logical Replication

Settings

Description Value Version
Primary Server 192.168.0.2 10
Standby Server 192.168.0.3 13
Default Port 5432

Configure postgresql.conf (PRIMARY)

wal_level = replica

Restart PostgreSQL

pg_ctl stop
pg_ctl status

Copy Schema Definitions

pg_dumpall -s >schemadump.sql
psql -d postgres -f schemadump.sql

Any schema changes after this point will not be replicated.

Create Replication User on Primary

CREATE ROLE repuser REPLICATION LOGIN PASSWORD '<password>';

Create Publisher on Primary

CREATE PUBLICATION pub_upgrade FOR ALL TABLES;

This needs to be done on every database in the instance.

Create Subscriber on Standby

CREATE SUBSCRIPTION sub_upgrade 
CONNECTION 'host=192.168.0.2 port=5432 dbname=doob user=repuser password='<password>' 
PUBLICATION pub_upgrade;
postgresqlreplication.1629899488.txt.gz · Last modified: 2025/03/08 22:23 (external edit)