Testing Your PostgreSQL Backups with an Automated Restore
A nightly pg_dump is only half a backup. How I verify dumps by restoring them into a throwaway container and alerting if it fails.
· 3 min read · Erik Kubica
Do You Know the Backup Restores?
I once reviewed the setup of a startup that had been running for three years. During the technical review, I asked the CTO about their disaster recovery strategy.
He was confident. "Every night at 03:00, a cron job on our database server runs pg_dump, gzips the output, and syncs it to an Amazon S3 bucket. We have 1,000 daily dump files stored offsite."
I asked a follow-up: "When was the last time you downloaded one of those files and restored it into a clean database?"
Nobody could remember. They had never tested a restore.
We downloaded the previous night's 450 MB dump file to a staging machine and ran pg_restore. It failed within a few seconds with a syntax error.
The cause was mundane. Eight months earlier, someone had upgraded PostgreSQL from version 14 to version 16 on the primary server, but the backup script was still calling an old PostgreSQL 12 pg_dump binary. The dumps in S3 for those eight months could not be restored.
They believed they had 1,000 backups. In practice they had a lot of files, and none of the recent ones were known to work. It is an easy mistake to make, and I have made similar ones with my own setups.
A backup is only known to work once you have restored it successfully.
What My Backup Automation Looks Like
I split the pipeline into two phases: creation and verification.
Here is how I create database dumps using the custom compressed format:
// Dump database using directory or custom format with parallel compression
pg_dump \
-h 127.0.0.1 \
-U app_admin \
-d app_production \
-Fc \
-Z 6 \
-f "/var/backups/pg_$(date +%Y%m%d_%H%M%S).dump"
The -Fc flag creates a custom archive format that includes an internal table of contents and supports parallel restoration via pg_restore -j 4.
The Nightly Verification Script
Creating the file is the easy half. The part I find more useful is the nightly verification loop.
On a separate staging verification server, a script runs every morning at 04:30:
// 1. Pull the latest dump from storage
LATEST_DUMP=$(ls -t /backups/*.dump | head -n 1)
// 2. Spin up an ephemeral PostgreSQL instance on a random port
docker run --name pg_verify_test \
-e POSTGRES_PASSWORD=verify_secret \
-d -p 5439:5432 postgres:16-alpine
// Wait for database engine readiness
sleep 5
// 3. Attempt full restore
pg_restore \
-h 127.0.0.1 -p 5439 \
-U postgres -d postgres \
--clean --if-exists \
"$LATEST_DUMP"
RESTORE_EXIT=$?
if [ $RESTORE_EXIT -ne 0 ]; then
curl -s -X POST "https://alerts.example.com/webhook" -d "BACKUP CORRUPT: Restore failed"
docker rm -f pg_verify_test
exit 1
fi
// 4. Run sanity assertion queries
USER_COUNT=$(psql -h 127.0.0.1 -p 5439 -U postgres -d postgres -t -c "SELECT count(*) FROM users;")
ORDER_COUNT=$(psql -h 127.0.0.1 -p 5439 -U postgres -d postgres -t -c "SELECT count(*) FROM orders;")
// 5. Tear down container and ping healthcheck
docker rm -f pg_verify_test
curl -s "https://uptime.example.com/api/push/BACKUP_OK?users=${USER_COUNT}&orders=${ORDER_COUNT}"
What the Check Gives You
The script covers a few things:
- It shows the dump file is not truncated or corrupted.
- It shows the
pg_restorebinary version is compatible with the dump file. - It shows table foreign keys, indexes, and constraints can rebuild without errors.
- It checks that the user and order counts are non-zero.
- If the restore fails, it sends an alert to my phone before 05:00.
On my setup the verification takes about three minutes of CPU time and costs less than 1 € a month in compute.
It will not catch everything, such as a dump that restores fine but is missing recent data, so the sanity queries are worth tailoring to your schema. Still, if you cannot say when your last restore succeeded, a small script like this is a good place to start.