Replication in PostgreSQL

suggest change

The steps below set up streaming physical replication with a hot standby. Two version boundaries matter: recovery.conf was removed in PostgreSQL 12, and wal_level value names changed in PostgreSQL 9.6. pg_basebackup talks to a running primary; do not stop the primary to take this backup.

Configuring the primary server

Requirements:

Create a replication user

createuser -U postgres -P -c 5 --replication replication

Create an archive directory

mkdir $PGDATA/archive

Edit pg_hba.conf

This is the host-based authentication file. It controls client authentication. Add an entry for the standby:

# hosttype    database_name     user_name       hostname/IP      method
host          replication       replication     <slave-IP>/32    scram-sha-256

replication in the database column is a special name, not a database you create. Use md5 only when the password is stored as MD5. Since PostgreSQL 14 the default password encryption is scram-sha-256, which does not work with md5. Reload the configuration after editing pg_hba.conf.

Edit postgresql.conf

wal_level = replica

wal_level controls how much is written to the WAL. replica is enough for a physical standby, including read-only queries. logical adds information for logical decoding. minimal is not enough for replication. Before PostgreSQL 9.6 the names were archive and hot_standby; those are still accepted and mapped to replica. There is no wal_level value named streaming.

archive_mode = on
archive_command = 'test ! -f /path/to/archivedir/%f && cp %p /path/to/archivedir/%f'

archive_mode allows WAL segments to be sent to the archive with archive_command. The command above copies a completed segment into the archive directory if that file is not already there.

max_wal_senders = 5

This is the maximum number of WAL sender processes. The parameter name is max_wal_senders, not wal_senders.

Restart the primary so these settings take effect. Leave it running for pg_basebackup.

Back up the primary onto the standby

pg_basebackup copies the data directory from the running primary to the standby data directory.

pg_basebackup -h <primary IP> -D /var/lib/postgresql/<version>/main -U replication -v -P -X stream

Do not start the standby until its configuration is in place.

Configuring the standby server

On PostgreSQL 11 and earlier, set hot_standby in postgresql.conf and put the connection settings in recovery.conf in the data directory:

hot_standby = on

hot_standby allows queries while the standby is recovering.

recovery.conf:

standby_mode = on
primary_conninfo = 'host=<primary-external-IP> port=5432 user=replication password=<password>'
trigger_file = '/tmp/postgresql.trigger.5432'

standby_mode keeps the server in recovery until you promote it. primary_conninfo is the connection string to the primary. The trigger_file path is optional: creating that file promotes the standby. pg_ctl promote does the same thing.

On PostgreSQL 12 and later there is no recovery.conf. Put primary_conninfo and hot_standby in postgresql.conf (or postgresql.auto.conf), and create an empty file named standby.signal in the data directory. promote_trigger_file replaces trigger_file. hot_standby already defaults to on.

Start the standby server

Start PostgreSQL on the standby. It should connect to the primary and begin replaying WAL. Check the standby log if it does not.

Attribution

This article is substantially derived from and attributed to How to Set Up PostgreSQL for High Availability and Replication with Hot Standby, with minor changes in formatting and examples and some text deleted. The source was published under the Creative Commons Public License 3.0, which is maintained here.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents