Sub topic 5
Starting the standby: standby.signal, primary_conninfo and hot_standby
This chapter covers the step after pg_basebackup: turning the copied data directory into a running hot standby. It assumes you can start and stop a server with pg_ctl or systemd, edit postgresql.conf, and use psql. It also assumes you have already prepared the primary and run pg_basebackup as shown in the previous chapters. The examples use PostgreSQL 12 or later, where recovery.conf no longer exists.
What turns a data directory into a standby
A copy of the primary's data directory is not yet a standby. If you start it unchanged, it runs crash recovery and opens as an independent read-write server. Three things change that:
standby.signal: an empty file in the data directory. At startup, PostgreSQL sees it and stays in recovery instead of opening for writes.primary_conninfo: a libpq connection string that tells the standby's walreceiver process where the primary is and how to authenticate.hot_standby: a setting that allows read-only connections while recovery is in progress.
The first two decide whether the server streams WAL at all. The third decides whether you can query it.
What pg_basebackup -R already did
If you ran pg_basebackup with -R, it did most of this work. It created an empty standby.signal and appended connection settings to postgresql.auto.conf. With -C -S standby1 it also created the slot on the primary. Inspect the result before starting:
ls -l /var/lib/postgresql/data/standby.signal
tail -n 5 /var/lib/postgresql/data/postgresql.auto.confThe tail will look roughly like this:
primary_conninfo = 'user=replicator password=change-me host=primary.example.com port=5432 sslmode=prefer'
primary_slot_name = 'standby1'Note that -R does not write application_name. Without it, the standby appears in pg_stat_replication under its default name, usually walreceiver. That matters later for synchronous replication, which matches on this name.
If you built the copy another way, create the pieces by hand:
touch /var/lib/postgresql/data/standby.signal
chown postgres:postgres /var/lib/postgresql/data/standby.signalConfiguring the connection
Put the settings in postgresql.conf or postgresql.auto.conf on the standby:
# postgresql.conf on the standby
primary_conninfo = 'host=primary.example.com port=5432 user=replicator application_name=standby1 sslmode=require'
primary_slot_name = 'standby1'
hot_standby = onSome details about primary_conninfo:
- It is an ordinary libpq string. Any libpq option works, including
sslmode,connect_timeout,target_session_attrsandkeepalives_idle. - You do not name a database. The walreceiver makes a replication connection, which matches the
replicationpseudo-database entry in the primary'spg_hba.conf. - Avoid putting the password in the string. Anyone who can read
postgresql.auto.confor runSHOW primary_conninfoas a superuser sees it. Use a~/.pgpassfile for the OS user that runs PostgreSQL instead. The line for this setup isprimary.example.com:5432:replication:replicator:change-me. The file must have mode0600, or libpq ignores it. primary_slot_nameis optional. Without it, the primary keeps WAL only as long aswal_keep_sizeallows, and a standby that falls too far behind cannot catch up by streaming.
Changing primary_conninfo and primary_slot_name requires only a reload on PostgreSQL 13 and later. On PostgreSQL 12, a restart is required.
hot_standby and the settings that must match
hot_standby defaults to on since PostgreSQL 10, but set it explicitly so the intent is visible. With it off, the standby replays WAL but rejects every connection until it is promoted.
A standby must be able to hold everything the primary could be running. Several parameters must be at least as large on the standby as on the primary:
| Parameter | Why the standby needs it |
|---|---|
| `max_connections` | Replayed WAL tracks the primary's transactions, and the standby's lock and snapshot structures are sized from this. |
| `max_worker_processes` | Same reason, for background workers. |
| `max_wal_senders` | Matters for cascading standbys and after promotion. |
| `max_prepared_transactions` | Prepared transactions on the primary must be representable. |
| `max_locks_per_transaction` | Lock table sizing for replayed locks. |
If the primary raises one of these and the standby has a lower value, the standby pauses replay when it hits the change. In PostgreSQL 13 and later it logs a FATAL-level message and shuts down. In either case, fix the standby's value and restart it. When you increase these values, restart the standbys first, then the primary. When you decrease them, do the primary first.
Starting and checking the standby
Make sure the data directory is owned by the PostgreSQL user and has mode 0700. Then start the server:
chmod 700 /var/lib/postgresql/data
pg_ctl -D /var/lib/postgresql/data startA healthy startup log looks like this:
LOG: entering standby mode
LOG: redo starts at 0/3000028
LOG: consistent recovery state reached at 0/3000100
LOG: database system is ready to accept read only connections
LOG: started streaming WAL from primary at 0/4000000 on timeline 1Each line marks a stage. "Consistent recovery state reached" means the standby has replayed enough WAL to match a valid point in time. Only then does it accept read-only connections. "Started streaming" confirms the walreceiver connected to the primary.
Confirm both sides. On the standby:
SELECT pg_is_in_recovery(),
pg_last_wal_receive_lsn(),
pg_last_wal_replay_lsn();pg_is_in_recovery() must return true. On the primary:
SELECT application_name, client_addr, state, sync_state
FROM pg_stat_replication;You should see one row with state = 'streaming'. A standby that is up but missing from this view is not connected. Check the standby log.
Common startup problems
no pg_hba.conf entry for replication connection: the primary has nohost replication ...line matching the standby's address and user. Add it and reload the primary.password authentication failed: the password is wrong, or.pgpasshas the wrong permissions or the wrong database field. The database field must be the wordreplication.requested WAL segment ... has already been removed: the primary recycled WAL before the standby fetched it. Use a replication slot, increasewal_keep_size, or add WAL archiving.replication slot "standby1" does not exist: the slot name does not match what exists on the primary. Checkpg_replication_slotsthere.- The server opens read-write and
pg_is_in_recovery()isfalse:standby.signalwas missing at startup. Stop it and do not let applications write to it. Its data has now diverged from the primary, so rebuild it withpg_basebackup. hot standby is not possible because ... is a lower setting than on the primary: one of the parameters in the table above is too low on the standby.
What you should not do on the standby
A hot standby is read-only. Writes, DDL, CREATE TEMP TABLE, and nextval() on sequences all fail with an error such as cannot execute INSERT in a read-only transaction. Roles, tables and settings come from the primary through WAL, so you cannot create a user on a standby only. Settings in postgresql.conf are the exception, because that file is not replicated. Memory settings, hot_standby and the connection strings are all local to each server.
Exercise
You start a new standby, and pg_is_in_recovery() returns true, but pg_stat_replication on the primary shows no rows. The standby log repeats "FATAL: no pg_hba.conf entry for replication connection from host 10.0.0.25, user replicator". Why can you still run queries on the standby, and what do you change to fix the connection?
Show answer
The standby is in recovery (standby.signal was found) and has reached a consistent state from the WAL in the base backup, so hotstandby lets it accept read-only queries. It has no streaming connection, so it is not receiving new WAL, and its data is stale. The primary has no matching host replication replicator <address> scram-sha-256 line in pghba.conf that covers 10.0.0.25. Add or correct the line (for example 10.0.0.0/24), reload the primary with SELECT pg_reload_conf();, and the walreceiver will connect on its next retry.