Skip to content
mentoor

Topic 5 of 5 · 5 min read

Backups and point-in-time recovery

Goal: Choose a backup setup that meets a stated recovery point and recovery time, and describe the restore test that proves it works.

A backup is only real once you have restored it, so pick the method from the recovery point and recovery time you need, then schedule a restore test. Recovery point objective (RPO) is how much recent data you can afford to lose. Recovery time objective (RTO) is how long you can be down.

Check yourself · 1 of 3

An engineer runs DELETE FROM orders without a WHERE clause. Why does a streaming replica not save the data?

Choose one answer

Why it matters

Data is lost by people more often than by disks: a bad migration, a DELETE without a WHERE, a script run against production. A replica copies those mistakes within seconds. A nightly dump loses up to a day of work. Matching the method to the objective is the whole job.

How to do it

  1. Write the numbers down. "Lose at most five minutes, be back within an hour" is a requirement. "Back up regularly" is not.
  2. Logical backups with pg_dump. The result is SQL or archive output for one database. It is portable across versions, good for small databases and for moving data. It captures one moment, so the recovery point is the time of the dump, and restoring a large database is slow because every index is rebuilt.
  3. Physical base backups plus WAL archiving. A base backup copies the data files. Archiving the write-ahead log (WAL) continuously lets you replay changes up to any chosen moment, which is point-in-time recovery (PITR). The RPO shrinks to the WAL archive lag, often seconds to minutes.
  4. Managed services. Cloud Postgres offerings usually run this for you and give a recovery window, such as restoring to any second within the last N days. Read the plan: the window length, whether recovery creates a new instance, and whether backups live in another region.
  5. Do not count replicas. A streaming replica applies every change, including the accidental DROP TABLE. It protects against hardware failure, not mistakes.
  6. Run the drill. On a schedule, restore into a scratch environment, recover to a chosen timestamp, run a few checks (row counts, a known recent record), and time it. That time is your real RTO.

Before and after

Weak plan:  nightly pg_dump to the same server's disk. Replica in another zone.
Fixed plan: weekly base backup + continuous WAL archive to object storage in another
            account, 14-day window. Monthly restore drill to a scratch instance,
            timed, with a row-count check. Dumps kept for migrations only.

What goes wrong

  • Backups on the same machine or account. One failure or one compromised credential takes both.
  • Never restoring. Corrupt archives, missing WAL segments and wrong permissions show up only at restore time.
  • Replica treated as backup. It faithfully copies the mistake.
  • Unknown restore time. A multi-terabyte dump restore can take far longer than the outage budget.

Examples

Choosing from the requirement

Requirement Method
Lose at most 24 hours, small database Nightly pg_dump to object storage
Lose at most 5 minutes, back in 1 hour Base backup + WAL archive, or a managed PITR window
Survive a zone outage Add a replica (availability), keep backups too

The second row cannot be met by dumps, because the dump interval is the data loss.

Recovering before a bad DELETE

recovery_target_time = '2026-12-01 14:29:00+00'
recovery_target_action = 'pause'

The DELETE ran at 14:30. Restoring the base backup and replaying WAL to 14:29 returns the data. The pause lets you check it before promoting the instance.

Key points

  • Define RPO (how much data you can lose) and RTO (how long you can be down) before choosing a backup method.
  • pg_dump is a logical snapshot at one moment: portable and simple, but its recovery point is the dump time and restores of large databases are slow.
  • A base backup plus continuous WAL archiving enables point-in-time recovery to a chosen moment before a mistake.
  • A streaming replica copies every change, including destructive ones, so it is for availability, not for backup.
  • Keep backups in a separate account or location from the database they protect.
  • Only a timed restore drill proves the backup works and tells you the real recovery time.

Common mistakes

  • Counting a read replica as a backup.
  • Storing dumps on the same server or in the same cloud account as the database.
  • Assuming a managed service's backups work without ever restoring one.
  • Choosing a method without a stated recovery point and recovery time.

Check yourself · 2 of 3

A business accepts losing at most 5 minutes of data. Which setup can meet that?

Choose one answer

Check yourself · 3 of 3

What does a restore drill give you that having backup files does not?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic