Recovering Postgres to a Point in Time After a Bad Deploy

Someone ran a DELETE without a WHERE, or a migration dropped the wrong column, and it is already committed. The rows are gone from the live database. If you have been archiving WAL, they are not gone from your backups — and you can rewind the whole cluster to the second before the mistake.

Point-in-time recovery (PITR) is the technique that makes this possible. It works because a physical base backup plus a continuous stream of write-ahead log (WAL) segments is enough to reconstruct the database at any moment the WAL covers. You restore the base backup, tell Postgres to replay WAL up to a chosen timestamp and stop, promote, and verify. This walkthrough uses PostgreSQL 17 and pgBackRest on Linux. It assumes you already have a working stanza with WAL archiving — if you do not, that is the baseline setup to get in place before you ever need this.

Find the exact target time first

The whole recovery hinges on one number: the moment just before the destructive statement committed. Get this wrong by a few seconds in either direction and you either replay the damage back in or lose good transactions that landed afterwards. Do not guess it. The Postgres log will tell you, provided you log statements or at least DDL and errors.

# Find when the bad migration ran. Adjust the log path for your install.
grep -i -n 'delete from orders' /var/lib/pgsql/17/data/log/*.log

# List the base backups you hold and the WAL range each one covers.
sudo -u postgres pgbackrest --stanza=main info

The log line gives you a timestamp with a timezone offset. That offset matters: pgBackRest and Postgres want the target expressed exactly as Postgres reckons time, including the zone. If your logs show 2026-10-21 14:32:10.104 UTC for the offending statement, your target is the moment before it — say 2026-10-21 14:32:09.500000+00. The info output confirms you hold a base backup taken before that time and unbroken WAL running past it. If the WAL archive has a gap between the backup and your target, replay will stop at the gap and this technique cannot reach the incident.

Restore the base backup and replay to the target

Stop the cluster before you touch its data directory. Restoring over a running Postgres is how a bad day becomes a corrupt one. Then run the restore with --type=time, pointing --target at the moment before the incident and asking pgBackRest to promote once it gets there.

sudo systemctl stop postgresql-17

# Restore the latest base backup, replay WAL up to one moment before the
# DELETE committed, then promote so the cluster comes up read-write.
sudo -u postgres pgbackrest \
  --stanza=main \
  --delta \
  --type=time \
  "--target=2026-10-21 14:32:09.500000+00" \
  --target-action=promote \
  restore

sudo systemctl start postgresql-17

Three flags carry the work. --type=time selects timestamp-based recovery. --target is the stop point. --target-action=promote tells Postgres to end recovery and open for writes once the target is reached, rather than sitting paused. The --delta flag is a convenience: instead of wiping and re-copying the whole data directory, pgBackRest compares checksums and only restores files that differ, which is far quicker on a large cluster. If you need a specific older backup rather than the most recent one, add --set=20261021-020000F using a label from the info output.

What pgBackRest writes, and the inclusive-target trap

pgBackRest does not hand-edit a config file you have to remember. It writes the recovery settings into postgresql.auto.conf and drops a recovery.signal file into the data directory, which is what tells PostgreSQL 12 and later to enter archive recovery on the next start. The generated block looks like this:

# Written by pgBackRest into postgresql.auto.conf
restore_command = 'pgbackrest --stanza=main archive-get %f "%p"'
recovery_target_time = '2026-10-21 14:32:09.500000+00'
recovery_target_action = 'promote'

One default catches people out. recovery_target_inclusive is on by default, meaning replay stops after the last transaction whose commit time is at or before your target. That is why you aim at the moment before the incident rather than at the incident itself — if you set the target to the exact commit time of the bad statement, an inclusive stop can replay it straight back in. Give yourself a margin below the offending commit, or set recovery_target_inclusive = off explicitly if you need to land on a precise boundary.

Verify before you let anyone back in

Promotion means the cluster is writable, not that the recovery did what you wanted. Check the log for the line confirming recovery stopped at your target, confirm the cluster is out of recovery, and — the part that actually matters — count the rows you were trying to save.

-- Should return false: recovery finished, cluster is read-write.
SELECT pg_is_in_recovery();

-- The rows the bad DELETE removed should be back.
SELECT count(*) FROM orders;

-- Sanity-check the newest surviving data predates the incident.
SELECT max(created_at) FROM orders;

The Postgres log will contain a line such as recovery stopping before commit of transaction ... and recovery has paused or, with promote, the point at which the new timeline begins. Keep that log. When someone asks later why some transactions from those final seconds are missing, the answer is that they committed after your chosen target and were deliberately left behind — the trade you made to remove the destructive change.

The drill is the control, not the command

None of the above is hard to type. What makes it work under pressure is having done it before, on this cluster, with this WAL archive, at a moment when nobody was shouting. A restore procedure that has never been rehearsed is a hypothesis, not a control. The teams that recover cleanly are the ones who run a scheduled PITR drill to a scratch host, time it, and record the result — the same recovery-time engineering discipline that turns a paper RTO into a number you can trust. It also keeps your change-failure recovery honest: a bad deploy that you can rewind in twenty minutes is a very different mean time to recovery from one you discover you cannot execute.

Archiving WAL is the insurance. The drill is the receipt that proves the policy pays out. Buy both before the migration that needs them.

Build and rescue work

Hands-on delivery of this kind is handled by Sixteen Pillars Studio.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.