PostgreSQL backups you can actually restore
How to back up PostgreSQL properly: logical dumps, point-in-time recovery, off-site copies and the automated restore test most teams skip.
A PostgreSQL backup only counts once you have restored it. Take nightly logical dumps with pg_dump -Fc, add continuous WAL archiving for point-in-time recovery on busy databases, copy everything off the server with encryption, and run an automated restore into a scratch database every week to prove the backups work.
The most common thing I find when taking over a client's server is a backup job that has been "running" for months and producing nothing useful: empty files, dumps of the wrong database, or archives stored on the same disk they are meant to protect. Here's the setup I use instead.
Logical dumps: the simple baseline
pg_dump exports a database as a consistent snapshot without stopping it. Use the custom format: it is compressed and lets you restore single tables.
pg_dump -Fc -d app_production -f /var/backups/pg/app_$(date +%F).dump
Remember that pg_dump backs up one database. Roles and other cluster-wide settings live elsewhere, so dump them too:
pg_dumpall --globals-only > /var/backups/pg/globals_$(date +%F).sql
Dumps are perfect for small and medium databases. Their weakness: you can only go back to the moment of the last dump. Lose the server at 5pm and a midnight dump costs you seventeen hours of data.
Point-in-time recovery for data you can't lose
For databases where losing hours of orders or messages is unacceptable, add continuous archiving. PostgreSQL writes every change to its write-ahead log (WAL). Archive those WAL files plus a periodic base backup, and you can restore to any second in between.
You can build this with archive_command and pg_basebackup, but I recommend a dedicated tool such as pgBackRest or WAL-G. They handle compression, encryption, retention, parallel restores and pushing straight to object storage, and they check the archive is complete.
Get it off the server
A backup on the same disk, or even the same provider account, protects against mistakes but not against the server, the disk or the account being lost. Follow the 3-2-1 rule: three copies, on two kinds of storage, one off-site.
A simple way to do this for dump files is restic, which encrypts, deduplicates and sends to any S3-compatible bucket:
restic -r s3:https://s3.example.com/backups-bucket backup /var/backups/pg
restic -r s3:https://s3.example.com/backups-bucket forget --keep-daily 14 --keep-weekly 8 --keep-monthly 12 --prune
Give the backup credentials permission to write but not to delete old snapshots where your storage supports it (object lock or versioning). Ransomware that reaches the server then can't wipe your history.
The step everyone skips: test the restore
Schedule a job that restores the latest dump into a throwaway database and checks the data looks real:
#!/usr/bin/env bash
set -euo pipefail
LATEST=$(ls -1t /var/backups/pg/app_*.dump | head -n1)
dropdb --if-exists restore_test
createdb restore_test
pg_restore --no-owner -d restore_test "$LATEST"
ROWS=$(psql -tA -d restore_test -c "SELECT count(*) FROM orders;")
if [ "$ROWS" -lt 1000 ]; then
echo "Restore test failed: only $ROWS orders in $LATEST" >&2
exit 1
fi
echo "Restore OK: $ROWS orders from $LATEST"
dropdb restore_test
Wire the failure into the same alerting you use for downtime. Pick a check that matters to the business, such as recent orders or users, not just "the command exited successfully". Time the restore too: that number is your real recovery time.
A sensible schedule
- Nightly
pg_dumpof each database plus globals. - Continuous WAL archiving for anything transactional.
- Off-site, encrypted copies with 14 daily, 8 weekly and 12 monthly snapshots.
- An automated restore test every week, and a full manual recovery drill every quarter.
FAQ
How often should I back up a PostgreSQL database?
At least daily for any business database. If losing a day of data is unacceptable, add continuous WAL archiving so you can recover to within seconds of a failure.
Are my cloud provider's disk snapshots enough?
They help, but a disk snapshot of a running database isn't guaranteed to be consistent unless the provider coordinates with PostgreSQL, and it usually lives in the same account. Keep database-native backups off-site as well.
How do I know my backups work?
Restore them automatically on a schedule into a scratch database and check real business data, such as a recent order count. A backup you haven't restored is only a hope.
Keep reading
- · 3 min Custom domains for multi-tenant SaaS with automatic HTTPS How to let every customer use their own domain on your SaaS, with certificates issued automatically by Caddy's on-demand TLS and a safe ask endpoint.
- · 3 min End-to-end encryption in the browser with the Web Crypto API A practical tutorial on encrypting messages in the browser with ECDH key agreement, HKDF and AES-256-GCM using the built-in Web Crypto API, plus the mistakes to avoid.