===== 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 -h -U -v -P -c fast -X stream -R -D Data Directory -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: \\ pg_basebackup: waiting for background process to finish streaming ... pg_basebackup: base backup completed ==== Configure postgresql.conf (STANDBY) ==== hot_standby = on ==== Create a Replication Slot on the Primary ==== SELECT * FROM pg_create_physical_replication_slot('primary_slot'); ==== Edit recovery.conf on Standby ==== standby_mode = on primary_conninfo = 'host=[Primary IP] port=5432 user=repmgr password=[Password]' primary_slot_name = 'primary_slot' ==== Start PostgreSQL on Standby Server ==== pg_ctl start pg_ctl status ==== Check Replication Progress ==== cd $HOME/11/main/pg_wal 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(); select pg_wal_replay_pause(); select pg_wal_replay_resume(); -- Note some function names changed in PostgreSQL 10 -- Run on subscriber server 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 restart pg_ctl status ==== Copy Schema Definitions ==== pg_dumpall -s > schemas.sql psql -d doob -f schemas.sql Any schema changes after this point will not be replicated.\\ ==== Create Replication User on Primary ==== CREATE ROLE repuser REPLICATION LOGIN PASSWORD ''; To copy the initial table data, the replication role must have SELECT privileges on all of the tables (or be a superuser).\\ ==== Create Publisher on Primary ==== -- All Tables CREATE PUBLICATION pub_upgrade FOR ALL TABLES; -- Selected Tables CREATE PUBLICATION pub_upgrade; ALTER PUBLICATION pub_upgrade ADD TABLE customers; ALTER PUBLICATION pub_upgrade ADD TABLE orders; 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='' PUBLICATION pub_upgrade; ==== Check Replication Progress ==== -- On Publisher \dRp SELECT * FROM PG_REPLICATION_SLOTS; SELECT * FROM PG_STAT_REPLICATION; -- On Subscriber \dRs ==== Issues ==== DDL changes will not be replicated to the subscriber.\\ May be possible to replicate via an Event Trigger?\\ [[https://www.enterprisedb.com/postgres-tutorials/how-use-event-triggers-postgresql]]\\ [[https://blog.dbi-services.com/can-i-do-it-with-postgresql-16-ddl-triggers/]]