Most Postgres setups I've seen have a backup job. Fewer have a restore job. The usual reason is that a restore test sounds like a weekend project: spin up a server, restore, poke around, tear it down. So it happens once, before an audit, and never again.

I wanted it to happen every night without me. This post is about what "the restore worked" should actually mean, and the checks I ended up with. The code is in an open-source tool called Restivra, but every check here works in a shell script too.

A clean restore can still be the wrong data

Three dumps that restore without an error:

  1. Yesterday's dump, written again today because the backup job silently failed and an old file was copied forward.
  2. A dump taken after someone truncated the events table. It is 70% smaller and perfectly valid.
  3. A dump from a replica that stopped replicating two days ago.

pg_restore is happy with all three. So the test has to compare the dump with something outside itself.

What I compare against

Its own history. Every run records the file's SHA-256, size and modification time. If the hash matches the last three runs, the dump is not changing. If the size falls under half the median of up to ten earlier runs, something upstream probably went wrong. Neither is a failure on its own, because a quiet database can produce identical dumps. They are warnings that show up in the report and can go to a webhook.

The data's own clock. A check like this catches the stale replica:

SELECT max(created_at) > now() - interval '26 hours' FROM orders; 

Things that must be true. Row counts that can't be zero, a reference table that must have a known row, a foreign key that should have no orphans. Each check is one SELECT that returns a single boolean. No semicolons, no comments, so a check can't turn into a script.

Restoring without trusting the dump

A dump is an input you didn't write, so the restore is sandboxed:

  • a fresh postgres:17 container per run, with networking disabled and a memory limit
  • a private copy of the dump; the original file is never mounted
  • checks run as a restricted role in a read-only transaction, with a 10 second statement timeout and a 2 second lock timeout
  • the container is removed at the end, pass or fail

The part that took longest was not the restore but recovery. Jobs, retries and leases live in Postgres. If the worker dies mid-restore, the next start finds the stale lease, cleans up the container and retries. CI kills the worker during a release on purpose to check that.

What it doesn't prove

A passing run proves that a logical dump restores into Postgres 17 and that your checks hold. It doesn't prove point-in-time recovery, that your app boots against it, or that the restore fits your RTO on production hardware. It narrows the gap; it doesn't close it.

Try it

Restivra is MIT-licensed and runs with Docker Compose: https://github.com/lazypanic999/restivra. It's an early pilot, PG 17 and pg_dump -Fc only. If a dump of yours breaks it, open an issue; that is the feedback I need most.

Full disclosure: I built it with a lot of help from AI coding tools. The integration tests, soak runs and fault-injection results are in the repo under docs/acceptance.

What do you check after a restore, if anything?