Sub topic 5
Taking a base backup with pg_basebackup
A base backup is the starting point for every point-in-time recovery (PITR). Archived WAL can only be replayed on top of a file-level copy of the cluster, and pg_basebackup makes that copy. This chapter assumes you can edit postgresql.conf and pg_hba.conf, and that you have already enabled WAL archiving as shown in the earlier chapter on continuous archiving. It also assumes you can run commands as the postgres operating system user.
What a base backup is
A base backup is a physical copy of the cluster's data directory, plus any tablespaces, taken while the server is running. It is not a logical dump like pg_dump. It copies files, so it restores to the same major version and the same platform architecture.
The files are copied while the database keeps changing, so the copy on its own is inconsistent. It becomes usable because WAL covering the copy period is available. During recovery, PostgreSQL replays that WAL and repairs the inconsistencies. The start of that WAL range is recorded in a file called backup_label.
Correction: the diagram must use the required fence. Here it is in its proper place below.
Prerequisites on the server
pg_basebackup connects through the replication protocol, so three things must be in place.
- A role with the
REPLICATIONattribute (or a superuser). - A
pg_hba.confentry that uses the special database namereplication. - Enough WAL sender slots.
max_wal_sendersmust be at least 2 when you use-X stream, because the WAL stream takes a second connection. The default is 10 in current versions.
CREATE ROLE replicator WITH LOGIN REPLICATION PASSWORD 'change-me';# pg_hba.conf
host replication replicator 127.0.0.1/32 scram-sha-256Reload the configuration after editing pg_hba.conf, for example with SELECT pg_reload_conf();. Keep the password out of scripts. Put it in ~postgres/.pgpass with mode 0600, or use a service file.
Running the backup
This is the command used throughout this tutorial.
pg_basebackup -h localhost -U replicator -D /backups/base/20261127 -Fp -X stream -c fast -PEach option has a reason.
| Option | Meaning | Why it matters for PITR |
|---|---|---|
| `-D dir` | Target directory | Must not exist, or must be empty. The tool refuses to overwrite data. |
| `-Fp` | Plain format | A ready-to-use copy of the data directory. `-Ft` writes tar files instead. |
| `-X stream` | Stream WAL during the backup over a second connection | The backup contains the WAL it needs to become consistent. |
| `-c fast` | Request an immediate checkpoint | The backup starts now. The default `spread` follows `checkpoint_completion_target` and can take minutes. |
| `-P` | Show progress | Useful on large clusters. |
The other -X modes are fetch and none. With fetch, WAL is collected at the end, so a busy server may recycle segments before they are copied and the backup fails. With none, the backup holds no WAL, so you rely entirely on the archive to make it consistent. For PITR, stream is the safest choice.
Do not use -R for PITR
The -R option writes connection settings and creates standby.signal. That is useful for building a streaming replica. A restored PITR copy that has standby.signal would start as a standby and never finish recovery in the way you expect. Leave -R out.
Running it as a repeatable script
A dated directory name keeps each backup separate. The -l option sets a label that is stored in backup_label.
#!/bin/bash
set -euo pipefail
BACKUP_DIR="/backups/base/$(date +%Y%m%d)"
pg_basebackup -h localhost -U replicator \
-D "$BACKUP_DIR" -Fp -X stream -c fast -l "base-$(date +%Y%m%d)"
pg_verifybackup "$BACKUP_DIR"Run this as the postgres user, or as another user that can write to /backups/base. The set -e line makes the script stop on the first failure, so a failed backup is never followed by a "success" message.
Looking inside the result
A plain-format backup holds the full data directory layout. Three items matter for recovery.
ls /backups/base/20261127
cat /backups/base/20261127/backup_labelSTART WAL LOCATION: 0/3000028 (file 000000010000000000000003)
CHECKPOINT LOCATION: 0/3000060
BACKUP METHOD: streamed
BACKUP FROM: primary
START TIME: 2026-11-27 12:00:01 UTC
LABEL: base-20261127
START TIMELINE: 1backup_labeltells recovery which checkpoint to start from. If you delete it, PostgreSQL starts from the control file's checkpoint, which may be wrong for a backup taken online. The result can be a corrupt cluster that starts without any error. Never delete or edit this file.pg_wal/holds the streamed segments. Recovery uses them first and then asksrestore_commandfor later ones.backup_manifest(PostgreSQL 13 and later) lists every file with its size and checksum.
The START TIME is the earliest moment you can recover to. A recovery target before the backup finished is rejected, because the copy is only consistent once the stop position has been replayed.
Verifying the backup
pg_verifybackup compares the files against backup_manifest and checks that the WAL needed to reach consistency is present and readable. In versions before 18 it works only on plain-format backups. Run it right after each backup. It catches truncated copies and storage corruption long before a real emergency does. It does not prove the cluster starts, which is the job of a test restore, covered in a later chapter.
Other formats and tablespaces
Tar format is convenient for shipping a backup to object storage.
pg_basebackup -h localhost -U replicator -D /backups/base/20261127-tar -Ft -z -X stream -c fastThis writes base.tar.gz and a separate tar file per tablespace, plus the WAL. You must unpack them before use. PostgreSQL 15 and later also offer --compress with choices such as server-gzip, so the server compresses the data before sending it, which saves network bandwidth.
In plain format, each tablespace is copied to its original absolute path. That fails if the path exists, and it would overwrite a live tablespace if you run the backup on the same host. Use -T to remap each one.
pg_basebackup -h localhost -U replicator -D /backups/base/20261127 \
-Fp -X stream -c fast -T /ssd/fast_ts=/backups/base/20261127-fast_tsWhen you restore, the symbolic links in pg_tblspc/ must point to where the tablespaces actually live on the restore host.
Impact on the running server
- The backup reads the whole cluster, so expect extra disk and network load. Limit it with
-r 50M(a rate in bytes per second, with suffixeskorM). - A fast checkpoint causes a burst of writes. Use
-c spreadfor a gentler start if the backup is not urgent. - While the backup runs, WAL is retained until the stream has read it. A slow client therefore does not break the backup, but it can slow the server's own recycling of segments.
- The backup must finish. If it is interrupted, delete the partial directory and start again. A half-copied directory looks like a backup but is not one.
Common errors
no pg_hba.conf entry for replication connection: thereplicationline is missing or the address does not match. Remember that the keywordallin the database column does not match replication connections.directory exists but is not empty: pick a new target directory.number of requested standby connections exceeds max_wal_senders: another client holds the slots. Raisemax_wal_senders(this needs a restart) or stop the other client.requested WAL segment has already been removed: this happens with-X fetchon a busy server. Switch to-X stream.
Exercise
You run pg_basebackup -D /backups/base/20261127 -Fp -X stream -R on a primary and later restore it for PITR with recovery.signal. The server does not behave like a normal recovery. Which option caused this, and what do you do instead?
Show answer
The -R option created standby.signal (and replication settings) in the backup, so the restored server starts as a standby instead of ending recovery at your target. Take the backup again without -R, or delete standby.signal from the restored directory before starting, and keep only recovery.signal.