Sub topic 12
Proving a Backup Works: Test Restores
This chapter assumes you know basic psql, pg_dump, pg_restore, PostgreSQL roles, and Linux command-line commands. The goal is to verify that a backup can produce a usable database, not merely that a backup file exists.
Why a backup file is not enough
A successful backup command proves only that PostgreSQL wrote data to a destination. The file might still be incomplete, unreadable, missing roles, or impossible to restore because the destination lacks required extensions or permissions.
A test restore is a restore performed in a separate environment. It checks the complete recovery path without changing the production database.
A useful verification process checks:
- The backup file can be read.
- The restore command completes successfully.
- Tables, indexes, sequences, and other objects exist.
- Important queries return expected results.
- Application-level checks pass.
- The restored database can be discarded without affecting production.
Check the backup before restoring
For a custom-format dump, pg_restore --list reads the archive table of contents without restoring it. This is a quick integrity check:
pg_restore --list mydb.dump | head -n 20The output should contain expected entries such as schemas, tables, sequences, and data. A readable table of contents does not guarantee a successful restore, so continue with a full test.
For a plain SQL dump, inspect the file and run a restore in a disposable database. Plain SQL files do not support pg_restore.
Restore into an isolated database
Create a temporary database owned by a test role or an administrative role. Do not restore over production. The destination should have enough storage and compatible PostgreSQL extensions.
createdb -U postgres backup_verify
pg_restore -U postgres -d backup_verify --no-owner --exit-on-error mydb.dumpThe --no-owner option prevents the restore from trying to assign objects to roles that may not exist in the test environment. The --exit-on-error option stops when a restore statement fails. Without it, some restore operations can continue after errors and make the result harder to interpret.
For a directory-format backup, give the directory to pg_restore instead:
pg_restore -U postgres -d backup_verify --no-owner --exit-on-error mydb_dirAfter the restore, run checks that reflect the database's real use. For example:
psql -U postgres -d backup_verify -v ON_ERROR_STOP=1 <<'SQL'
SELECT current_database();
SELECT count(*) AS customer_count FROM public.customers;
SELECT count(*) AS order_count FROM public.orders;
SELECT pg_size_pretty(pg_database_size(current_database()));
SQLCompare these results with known production expectations or a previously recorded baseline. Counts alone can miss corruption or missing indexes. Check representative queries, constraints, permissions, and extensions as well.
| Check | What it detects |
|---|---|
| Table and row counts | Missing or incomplete data |
| Representative queries | Unusable schema, data, or indexes |
| Extension list | Missing database dependencies |
| Role and privilege checks | Missing access configuration |
| Application smoke test | Failures visible only through normal use |
Clean up and record evidence
When verification finishes, remove the temporary database only after saving the results:
dropdb -U postgres backup_verifyRecord the backup identifier, creation time, PostgreSQL version, restore duration, database size, checks performed, and any warnings. Run this process on a schedule. A backup that worked last month can fail after a schema change, storage change, expired credential, or altered retention policy.
A practical policy is to test every new backup and perform a full restore on a regular schedule. Keep at least one test environment separate from the production host so a host failure cannot destroy both the backup and its verification target.
Exercise
Why should a custom-format dump be restored into a separate database with --exit-on-error?
Show answer
The separate database protects production, while --exit-on-error exposes the first restore failure instead of allowing a partially restored database to appear successful.