Sub topic 5
Taking a Base Backup with `pg_basebackup`
Assume you can start and stop PostgreSQL, edit postgresql.conf, and use the Linux shell. You also need a replication connection that has permission to run base backups. This chapter uses PostgreSQL 12 or later and a local server named localhost.
What the base backup contains
A physical base backup is a copy of the PostgreSQL cluster's data files. It gives recovery a known starting state. Archived WAL (write-ahead log) segments then replay every database change made after that starting point.
The backup and WAL must come from the same cluster. A base backup from one server cannot be combined with WAL from another server, even if both servers use the same database name.
The backup directory should be on storage with enough space for the complete cluster. Do not place it inside the live data directory. A backup stored on the same disk is useful for some mistakes, but it does not protect against disk failure.
Prepare the replication connection
The account running pg_basebackup needs the REPLICATION attribute or equivalent permissions. The server's pg_hba.conf must also allow that account to connect with the replication database target.
For example, a suitable role can be created by an administrator:
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'use-a-secret-from-your-secret-store';Use TLS and a protected password mechanism in production. Do not place a real password in shell history or tutorial scripts.
Run a streaming base backup
This command creates a plain-format backup and streams the WAL generated during the backup:
pg_basebackup -h localhost -U replicator \
-D /backups/base/20261127 \
-Fp -X stream -c fast -PThe important options are:
-Dselects the destination directory.-Fpcreates ordinary files that can be restored directly.-X streamstreams required WAL through a second replication connection while the backup runs.-c fastrequests a fast checkpoint. This can increase short-term I/O, so schedule it carefully on a busy server.-Pdisplays progress.
The destination must not contain an unrelated cluster. Check its ownership and permissions before using it. The PostgreSQL service account must be able to read the restored files, and the data directory normally needs permissions such as 0700.
Understand WAL coverage
-X stream makes the backup self-consistent by including the WAL needed to interpret changes made while PostgreSQL copied the data files. It does not replace continuous archiving. Keep archiving enabled so recovery can continue beyond the end of the backup.
If the streaming connection fails, pg_basebackup can fail or stop receiving WAL. Check the command's exit status and verify that the expected directory and files exist. A directory that was created successfully is not proof that the backup completed correctly.
A successful backup should also be copied or replicated to independent storage. Record its start time, end time, PostgreSQL version, cluster identity, and the range of WAL retained with it.
Common errors
| Error | Why it matters | Safer response |
|---|---|---|
| Backing up without streamed or archived WAL | Recovery may lack required changes | Use `-X stream` and keep archive WAL |
| Reusing a nonempty destination | Files from different backups can be mixed | Use a new, verified directory |
| Saving only on the database host | Host failure can destroy both copies | Copy backups to separate storage |
| Ignoring disk space | The backup can stop partway through | Check capacity before starting |
Exercise
Why is -X stream important when taking a base backup, and why does it not eliminate the need for WAL archiving?
Show answer
It streams the WAL needed to make this backup consistent while it is being copied. Continuous archiving is still needed to recover to points after the backup and to protect against a later failure.