DBAtlas · Database operations atlas

Configure a standby manually (standby.signal + primary_conninfo)

PostgreSQLReplication

Command

# On the standby, with PostgreSQL stopped (PGDATA came from a backup/copy):
touch $PGDATA/standby.signal
cat >> $PGDATA/postgresql.auto.conf <<'EOF'
primary_conninfo = 'host=<primary_host> port=5432 user=replicator application_name=<standby_name>'
primary_slot_name = '<slot>'
EOF
pg_ctl -D $PGDATA start
# PG13+: change the connection on a running standby without restart (e.g. point it to a new primary):
psql -X -c "ALTER SYSTEM SET primary_conninfo = 'host=<new_primary_host> port=5432 user=replicator application_name=<standby_name>'" -c "SELECT pg_reload_conf();"

What it does

The standby.signal file makes the server start as a standby; primary_conninfo defines the streaming connection and primary_slot_name the replication slot to use. On PG13+ primary_conninfo and primary_slot_name can be changed with a reload.

When to use it

To turn a data directory prepared without pg_basebackup -R (pgBackRest, snapshot, rsync) into a standby, or to point an existing standby to a new primary.

Caution

The slot must be created on the primary beforehand (pg_create_physical_replication_slot), otherwise the walreceiver keeps failing. Put the password in ~/.pgpass instead of conninfo. application_name must exactly match the name in synchronous_standby_names. If standby.signal is forgotten, the server starts as a primary and moves to a separate timeline.

Open in DBAtlas →

Related