Skip to the workspace
Pebellslearn by building
Backups and Recovery  ›  Lesson

Physical Backups with pg_basebackup

pg_basebackup 15 minpg_basebackup copies the whole cluster's files, and together with archived WAL it allows point-in-time recovery on the same major version.

postgresql dba / Physical Backups with pg_basebackup

Explore each scene at your own pace.

01 · The rule

The rule

Physical Backups with pg_basebackup

Pedal's database has grown to 400 GB. A logical restore now takes six hours, rebuilding every index, and the nightly dump can only give back 02:00. The business wants the server back within an hour, to within minutes of a failure. That needs a physical backup.

The rule

Copy the whole cluster's files, keep the WAL, replay to any moment.

pg_basebackup makes a byte-for-byte copy of the entire cluster: every database, the roles, everything in the data directory. It runs while the server is live and uses the replication protocol, so it needs:

  • a role with the REPLICATION attribute, and
  • a pg_hba.conf line with replication in the DATABASE column for that role.
pg_basebackup -h db1 -U replicator \
  -D /backups/base-2026-09-25 \
  -Fp -Xs -P
  • -D: where to write the backup.
  • -Fp: plain format, a ready-to-use copy of the data directory (-Ft -z would write compressed tar files instead).
  • -Xs: stream the WAL generated during the copy, so the backup is consistent on its own.
  • -P: show progress.

It also writes a backup_manifest listing every file with a checksum, used later to verify the backup.

Because the files are copied as they are, restoring is fast — no index rebuilds — but the backup only works with the same major version and a compatible platform.

Keep the WAL. A base backup alone is one moment again. Turn on WAL archiving and every completed WAL segment is copied somewhere safe:

# postgresql.conf
archive_mode = on        # needs a restart
archive_command = 'test ! -f /archive/%f && cp %p /archive/%f'

Replay to any moment. To recover, restore the base backup, set restore_command to fetch archived WAL and recovery_target_time = '2026-09-25 14:05:00+05:30', create a file named recovery.signal in the data directory and start the server. It replays WAL up to that moment. This is point-in-time recovery (PITR).

In practice, tools such as pgBackRest, Barman and WAL-G wrap these steps with scheduling, retention, compression and cloud storage. They all rest on this same idea. PostgreSQL 17 also added incremental base backups (--incremental), which copy only what changed since an earlier backup.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.