Sub topic 5
Starting the Standby: `standby.signal`, `primary_conninfo`, and `hot_standby`
This chapter assumes you can edit PostgreSQL configuration files, use psql, start and stop PostgreSQL, and have already configured a primary server. It also assumes you have created a base backup with pg_basebackup.
A standby starts in recovery mode. It continuously receives WAL from the primary, replays those changes locally, and can optionally accept read-only connections while recovery continues.
The standby startup sequence
Three settings control the basic connection and read behavior:
| Setting | Purpose |
|---|---|
| `standby.signal` | Selects standby mode when PostgreSQL starts |
| `primary_conninfo` | Describes how to connect to the primary |
| `hot_standby` | Allows read-only sessions during recovery |
The standby.signal file is an empty marker file in the standby's data directory. Its contents do not matter. Its presence tells PostgreSQL to start as a standby instead of starting as an independent server.
The standby then uses primary_conninfo to connect to the primary's replication endpoint. After connecting, the standby's WAL receiver receives records, and its recovery process replays them.
Configure the standby
The following settings can be placed in postgresql.conf or written by pg_basebackup -R to postgresql.auto.conf. The primary_slot_name setting is needed only when the primary has a matching physical replication slot.
# postgresql.conf on the standby
primary_conninfo = 'host=primary.example.com user=replicator password=change-me application_name=standby1'
primary_slot_name = 'standby1'
hot_standby = onThe application_name value identifies this standby in the primary's monitoring views. If you use a replication slot, the name in primary_slot_name must match the slot name exactly.
Avoid storing a real password in a configuration file when possible. Use a .pgpass file owned by the PostgreSQL operating-system user, and set its permissions so that only that user can read it.
Create the mode marker in the standby data directory:
touch /var/lib/postgresql/data/standby.signal
chmod 600 /var/lib/postgresql/data/standby.signalThe data directory must belong to the PostgreSQL operating-system user. Start the server using the service manager or pg_ctl, according to your installation.
pg_ctl -D /var/lib/postgresql/data \
-l /var/lib/postgresql/data/logfile startA simpler method is to let pg_basebackup create the marker and connection settings while it copies the primary:
pg_basebackup -h primary.example.com -U replicator \
-D /var/lib/postgresql/data -X stream -R -C -S standby1 -PThe target directory must be empty before this command runs. The -R option writes recovery connection information and creates standby.signal. The -C and -S options create and use the standby1 replication slot.
Verify standby mode
Connect to the standby and check its recovery state:
SELECT pg_is_in_recovery();The result should be t. While this value is true, the server is replaying WAL and accepts only operations allowed during recovery. A normal write such as INSERT, UPDATE, or CREATE TABLE fails.
A standby may be temporarily unable to connect if the primary is down or the network blocks the PostgreSQL port. It will keep retrying according to its connection settings. Check the PostgreSQL server log for authentication, DNS, firewall, or permission errors.
Exercise
Which setting allows clients to run read-only queries while the standby is replaying WAL?
Show answer
hot_standby = on