Sub topic 4
Cloning the Primary with `pg_basebackup`
Assume you understand PostgreSQL transactions, WAL, psql, Linux file permissions, and basic primary-standby terminology. This chapter focuses on creating the standby's initial data directory. It does not cover every setting needed to start the standby.
A standby cannot begin with an empty directory. It needs a consistent copy of the primary's database files, followed by every WAL record generated after the copy began. pg_basebackup performs both jobs. It connects to the primary, copies the data directory, and either streams or fetches the required WAL.
Requirements before the backup
The primary must allow replication connections. A dedicated login role with the REPLICATION attribute is the usual choice:
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'change-me';The primary also needs enough WAL senders and a replication connection rule. The replication value is a special pseudo-database name in pg_hba.conf.
# postgresql.conf on the primary
wal_level = replica
max_wal_senders = 10
# pg_hba.conf on the primary
host replication replicator 10.0.0.0/24 scram-sha-256Reload or restart PostgreSQL as required by the settings you changed. Confirm that the standby host can reach the primary's PostgreSQL port, usually 5432. Store the replication password securely rather than placing it directly in shell history.
Running `pg_basebackup`
Stop the PostgreSQL instance that will use the destination directory, and make sure the directory is empty. The PostgreSQL service account must own the directory and have permission to write it.
This command creates a physical base backup:
pg_basebackup -h primary.example.com -U replicator \
-D /var/lib/postgresql/data \
-X stream -R -C -S standby1 -PThe important options are:
| Option | Purpose |
|---|---|
| `-h`, `-U` | Select the primary host and replication user |
| `-D` | Choose the destination data directory |
| `-X stream` | Stream the required WAL through the backup connection |
| `-R` | Write standby connection settings and create `standby.signal` |
| `-C -S standby1` | Create and use the replication slot named `standby1` |
| `-P` | Show progress |
-X stream keeps the copy consistent even while the primary accepts writes. The backup process receives a starting position, copies database files, and obtains WAL covering changes made during the copy.
-R writes connection information into postgresql.auto.conf and creates an empty standby.signal file. These files tell PostgreSQL to start in standby mode. Without -R, you must create the signal file and configure the connection yourself.
Replication slots and backup safety
A physical replication slot records how much WAL a standby still needs. The primary retains WAL from the slot's restart position. This protects a delayed or disconnected standby from missing WAL, but an abandoned slot can fill the primary's disk.
Use a slot with a managed standby, and monitor it after the backup:
SELECT slot_name, active, restart_lsn, wal_status
FROM pg_replication_slots;Do not reuse a destination directory containing an unrelated PostgreSQL cluster. A base backup replaces the expected cluster contents, so verify the host, path, and service state before running the command.
Exercise
Why does pg_basebackup need WAL in addition to copying the primary's data files?
Show answer
The files are copied while transactions continue. WAL records describe changes made during the copy and let the standby replay them to reach a consistent state.