← Back to PostgreSQL, page 5

postgresql-0089 · Hard senior

Design a backup and recovery strategy that can restore a production database to any point in the last 14 days.

Point-in-time recovery needs periodic physical base backups plus continuous WAL archiving; restoring a base backup and replaying WAL up to a target time recovers to that moment. Tools such as pgBackRest or Barman manage retention, compression, encryption and verification. pg_dump logical backups complement this for single-table restores and migrations but cannot do PITR. Test restores regularly and measure recovery time.

Full answer

Building blocks:

  • Base backups: full physical copies (pg_basebackup, or pgBackRest full and incremental backups; PostgreSQL 17 adds native incremental backups with pg_combinebackup), taken daily or weekly.
  • WAL archiving: archive_command or archive_library ships every completed WAL segment to durable storage in another location; archive lag is monitored.
  • Retention: keep base backups and all WAL since the oldest one you may restore from, here at least 14 days plus one full backup cycle.
  • Recovery: restore the nearest base backup before the target, set recovery_target_time, and replay WAL; the server stops at the target and can be promoted.

Targets to agree on: RPO (how much data may be lost, driven by archive frequency or synchronous replicas) and RTO (how long a restore may take, driven by database size, backup format and WAL volume).

Practices that matter more than the tool: automated restore tests into a scratch environment with checks that the data is sane, encryption and access control for the backup bucket, alerts on failed archiving, and a written runbook. Replicas are not backups, because an accidental DELETE replicates immediately; delayed replicas (recovery_min_apply_delay) can help for quick recovery from such mistakes.

This item describes infrastructure and has nothing to run in a single schema.

Key points

  • PITR needs base backups plus WAL archiving
  • Restore to recovery_target_time
  • Define RPO and RTO
  • Test restores regularly
  • Replicas are not backups

Common mistakes

  • Relying only on nightly pg_dump
  • Never testing a restore

Follow-ups

  • How do you recover a single dropped table quickly?
  • What does PostgreSQL 17's incremental backup change?

Tips

  • State RPO and RTO