Verify PostgreSQL backups by restoring into a separate test database
A database backup job can exit successfully and leave an archive in the expected directory, yet a restore may reveal missing extensions, roles, or application data. Restore validation is an easy step to overlook when operating a dedicated server. A backup should count as complete only when it can be restored and the result is usable—not merely when a file exists. Using PostgreSQL on Debian 12 as an example, this guide walks through a logical backup and restore check without overwriting the production database.
1. Define what the backup covers
HubAMD dedicated servers come with two 1TB NVMe drives, each directly connected, with no hardware RAID. Keeping the database on one drive and the archives on the other separates their file locations, but both remain exposed to a failure of the server as a whole. Users are responsible for implementing redundancy and backups, and a separate test database is no substitute for a copy on another server.
pg_dump takes a consistent snapshot of a single database while allowing normal reads and writes to continue. However, it can wait for—or cause delays to—schema changes that require exclusive locks. Avoid table schema migrations during the backup, and record the database version, installed extensions, and application version.
A logical archive does not include all roles in the database cluster, server configuration, application secrets, or external files. Workloads that require point-in-time recovery also need a separate plan for base backups and WAL archiving; the procedure here does not provide that capability.
File verification answers “Have the bytes changed?” A restore drill answers “Is the archive usable?” Application-level validation answers “Does the restored data meet the application's requirements?” None of these three checks can replace the others.
2. Create the archive and checksum manifest
Run the following commands in an administrative Bash session. They assume that the local postgres system account can connect to the database through local authentication. Check the client tool version first; using the same major version as the source server is recommended. An older pg_dump cannot back up a server running a newer major version.
sudo -i
set -euo pipefail
umask 077
pg_dump --version
sudo -u postgres psql -X -d postgres -Atc 'SHOW server_version;'
read -r -p 'Database name: ' DB
test -n "$DB"
install -d -o postgres -g postgres -m 0700 /var/lib/postgresql/backup-check
BACKUP=$(mktemp -d /var/lib/postgresql/backup-check/run.XXXXXX)
chown postgres:postgres "$BACKUP"
sudo -u postgres pg_dump \
--dbname="$DB" \
--format=custom \
--file="$BACKUP/database.dump"
sudo -u postgres pg_dumpall \
--globals-only \
--file="$BACKUP/globals.sql"
cd "$BACKUP"
sha256sum database.dump globals.sql > SHA256SUMS
sha256sum -c SHA256SUMS
Enter the actual database name. Use a plain database name, not a connection string. The commands use the custom archive format, which supports inspecting the archive's table of contents and restoring individual objects. If any step fails, stop the validation process and do not register leftover files as a successful backup.
globals.sql may contain role password hashes. Protect it as sensitive credential material and never put it in a public repository. It is not part of the same atomic snapshot as the database archive, so avoid role changes while the backup is running. Record tablespace paths separately; do not assume the original paths will exist when restoring on another server.
After generating the files, also record the start and end times, command exit statuses, file sizes, and application version. Keep the checksum manifest through a controlled channel. If both the files and the manifest are modified, running the checksum check alone cannot detect that situation.
3. Restore into a separate test database
Continue in the same Bash session, keeping the variables defined earlier. Create the test database with a new name and an empty template. If creation fails, investigate the cause; do not substitute the production database name and continue.
VERIFY="backup_verify_$(date +%s)"
sudo -u postgres createdb --template=template0 "$VERIFY"
sudo -u postgres psql -X -v ON_ERROR_STOP=1 \
--dbname="$VERIFY" -v db="$VERIFY" \
-c 'REVOKE CONNECT ON DATABASE :"db" FROM PUBLIC;'
sudo -u postgres pg_restore \
--list "$BACKUP/database.dump" \
> "$BACKUP/archive-list.txt"
sudo -u postgres pg_restore \
--exit-on-error \
--no-owner \
--no-privileges \
--dbname="$VERIFY" \
"$BACKUP/database.dump"
This procedure does not use --clean, to avoid making “delete objects first, then import” a routine practice. Revoke public connection privileges on the test database, do not point the production application's connection configuration at it, and do not start scheduled jobs or message consumers. Superusers can still connect, so this is operational isolation, not a security sandbox.
--no-owner and --no-privileges simplify restoring the contents, but they do not validate production permissions. Do not import globals.sql directly into a running production cluster just to supply missing roles. Rehearse a full role restore in another controlled cluster.
Being able to read the archive's table of contents does not mean its objects can be created. Before restoring, check the target environment's extension software, encoding, and collation. If the source database uses extensions, a successful restore on the same server is especially poor evidence that another server has all the required dependencies. Use the same major version for the first drill where possible, and check compatibility separately for cross-version migrations.
4. Validate objects and application results
A restore command that completes without errors is only the first result to check. Start by listing user tables, then compare the restored objects with the inventory recorded from the source database. Look for missing schemas, views, functions, indexes, and extensions.
sudo -u postgres psql -X -v ON_ERROR_STOP=1 \
--dbname="$VERIFY" \
-c "SELECT schemaname, tablename
FROM pg_catalog.pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
ORDER BY schemaname, tablename;"
sudo -u postgres vacuumdb --analyze --dbname="$VERIFY"
Generate statistics after the restore, then inspect query plans. This helps avoid mistaking slow queries caused by missing statistics for a problem with the archive. Application-level validation should cover at least the following:
- Confirm that critical tables exist and that unique constraints and foreign keys have been restored.
- Check that known historical records can be queried, and that amounts, statuses, and relationships match expectations.
- Verify that sequences are correct. You can try inserting records in a test transaction and roll it back afterward.
- In a separate permissions drill, check whether application roles can perform the required operations and whether they have excessive privileges.
For a production database receiving ongoing writes, restored row counts cannot be compared directly with “current production row counts.” An exact comparison requires the same snapshot basis, or fixed batches and historical records supplied by the application that no longer change. Do not report data loss simply because normal new writes have occurred.
5. Make validation repeatable
After transferring the archive, manifest, and necessary configuration to another server, verify the checksums again, then restore in the target environment. A successful transfer does not prove that the copy can be restored. A disk image is also no substitute for protection against accidental deletion and retention of historical versions.
Track automated jobs through four stages: “export succeeded, checksum verification succeeded, restore succeeded, application validation succeeded.” If validation fails, preserve the logs and archives; automatic cleanup must not remove evidence. Run cleanup rules independently, and do not use ambiguous wildcards that could also match the newly generated backup.
When testing is complete, confirm the database name and close test connections before dropping that test database. Do not write cleanup scripts that iterate over every database. Restore records should include the archive identifier, tool versions, extension dependencies, and check results, so that an incident can be handled using verified steps rather than by guessing the restore order on the spot.
Frequently asked questions
Does a matching SHA-256 checksum prove a database can be restored?
No. It only confirms that the file bytes match the recorded checksum. Run pg_restore and check database objects, business data, and application permissions as well.
Can a successful same-server restore replace an off-server backup?
No. It validates the restore procedure but leaves production and backups exposed to the same machine failure. Keep the archive, checksums, and required configuration elsewhere and rehearse restoration there.
HubAMD AMD EPYC 7543P dedicated server
From $448/month