Overview

A backup you have not restored is a hope, not a backup. PostgreSQL offers logical backups (SQL or custom-format dumps of objects) and physical backups (a copy of the data directory plus archived WAL). Physical backup with WAL archiving enables point-in-time recovery (PITR): restore a base backup, then replay WAL to a chosen moment.

Core concepts

Logical (pg_dump)Physical (pg_basebackup, pgBackRest, Barman)
UnitOne database or objectsWhole cluster
Version portabilityRestores to newer major versionsSame major version and platform
Speed on large dataSlow (rebuilds indexes)Fast
PITRNoYes, with WAL archive
Use forMigrations, small databases, single-table recoveryProduction disaster recovery
  • WAL archiving: archive_mode = on and archive_command (or archive_library) copy each completed WAL segment to durable storage. A failing archive command makes WAL pile up on the primary.
  • Recovery: restore the base backup, create recovery.signal, set restore_command and a target (recovery_target_time, recovery_target_lsn, recovery_target_name), start the server, then pg_wal_replay_resume()/promote.
  • Targets: RPO (how much data you can lose) is set by WAL archive frequency or synchronous replication; RTO (how long recovery takes) by backup size, restore speed and WAL to replay.
  • PostgreSQL 17 adds incremental base backups: pg_basebackup --incremental with pg_combinebackup to reconstruct a full backup.
  • Tools such as pgBackRest and Barman add parallel backup and restore, compression, encryption, retention policies, verification and object-store targets.

Worked example

Logical backup and restore (executed commands are standard; sizes depend on your data):

pg_dump -Fc -h db -U app mydb > mydb.dump               # custom format, compressed, selectable restore
pg_restore -h newdb -U app -d mydb --jobs 4 mydb.dump    # parallel restore
pg_dumpall --globals-only > globals.sql                  # roles and tablespaces

PITR outline (manual; not executed here):

# primary postgresql.conf
archive_mode = on
archive_command = 'test ! -f /archive/%f && cp %p /archive/%f'
 
# on the restored copy, postgresql.conf
restore_command = 'cp /archive/%f %p'
recovery_target_time = '2025-03-01 09:14:00+00'
recovery_target_action = 'promote'

then touch recovery.signal in the data directory and start the server. Recovery stops at the target moment, just before the bad DROP TABLE.

Pitfalls and production notes

  • Test restores on a schedule and time them; record the measured RTO.
  • Keep backups and WAL archive in a different failure domain than the server; encrypt them and restrict access.
  • Monitor pg_stat_archiver (failed_count, last_failed_time) so archiving failures alert before the disk fills.
  • pg_dump takes a consistent snapshot but a long dump holds back vacuum; run it against a replica when possible.
  • After PITR to a past point a new timeline starts; take a fresh base backup.
  • Backups of encrypted columns or external files (large objects stored outside) need their own handling.

Practice questions

  1. A developer dropped a table at 10:32. Describe the steps to recover it without losing other data.
  2. Why can you not use pg_basebackup output to upgrade to the next major version?
  3. How do you decide the archive frequency from an RPO of five minutes?

References

pg-wal-checkpoints · pg-replication-failover · pg-security-roles