~/t/g/self-hosting $

Postgres backup untested? Restore and compare the data

gist Make a custom-format dump, restore into a separate instance, and compare data before adding a schedule.

contents
  1. Step 1: Prepare two separate instances
  2. Step 2: Make a custom-format dump
  3. Step 3: Restore into a separate test instance
  4. Step 4: Compare rows and check the application
  5. Step 5: Cover roles, files, and scheduled backups
  6. Compare backup approaches
  7. Common questions

If your Postgres backup has never been restored, use pg_dump and pg_restore on a separate instance and compare the recovered data.

I run Metabase with Postgres on my Unraid box. If that database goes, the dashboards go with it.

Use generated data, a separate restore instance and an empty recovery database for this rehearsal.

Step 1: Prepare two separate instances

Prepare:

  • A disposable source Postgres instance with an empty database.
  • A second disposable instance with its own data volume.
  • Postgres client tools matching the server’s major version.
  • A private backup directory with enough free space.

Set SOURCE_CONTAINER and RESTORE_CONTAINER in your shell to the two container names. Configure each container’s client connection settings privately. The source needs PGDATABASE set to the empty demo database. The restore connection must point to the second instance.

Use a password file inside each container when password authentication is required. PGPASSFILE can select its location. On Unix, the file must have permissions such as 0600, or Postgres ignores it. Shell environment variables are not automatically passed into an existing container. Postgres password-file documentation, Docker exec documentation.

Use matching major versions for this rehearsal. pg_dump refuses to dump a server newer than its own major version. Postgres pg_dump documentation.

Create the sample table in the disposable source database:

docker exec -i "${SOURCE_CONTAINER:?Set SOURCE_CONTAINER}" \
  psql -X --set=ON_ERROR_STOP=1 <<'SQL'
CREATE TABLE weekly_orders (
  id integer PRIMARY KEY,
  day text NOT NULL,
  orders integer NOT NULL
);

INSERT INTO weekly_orders VALUES
  (1, 'Monday', 12),
  (2, 'Tuesday', 18),
  (3, 'Wednesday', 9);
SQL

Keep this source unchanged until the row comparison is finished.

Step 2: Make a custom-format dump

pg_dump takes a consistent snapshot of one database while ordinary reads and writes continue. --format=custom produces a compressed archive for pg_restore. Postgres pg_dump documentation.

Run this from the directory where you keep the rehearsal’s files:

(
  set -eu
  umask 077
  mkdir -p backups

  final="backups/postgres-$(date -u +%Y%m%dT%H%M%SZ)-$$.dump"
  partial="${final}.partial"

  trap 'rm -f "$partial"' EXIT
  trap 'exit 1' HUP INT TERM

  docker exec "${SOURCE_CONTAINER:?Set SOURCE_CONTAINER}" \
    pg_dump --format=custom --no-password \
    > "$partial"

  mv "$partial" "$final"
  printf 'Archive saved: %s\n' "$final"
)

The final filename appears only after the dump succeeds. A failed export leaves earlier backups in place. Check the command’s exit status and any warnings.

Keep -t off binary dump exports. That option allocates a pseudo-terminal. The dump should go straight to the file. Docker exec options.

Set ARCHIVE to the saved file’s path. Inspect its contents:

docker exec -i "${SOURCE_CONTAINER:?Set SOURCE_CONTAINER}" \
  pg_restore --list < "${ARCHIVE:?Set ARCHIVE}"

For the sample database, look for the weekly_orders table, table data, and primary-key constraint. Keep any shared listing excerpt to those entries, with owner names removed.

Continue with an actual restore after checking the archive listing.

The custom Postgres archive lists weekly_orders, its table data and primary-key constraint
The archive listing shows the table, its data and its primary key. Readable, but not yet proof it restores.

Step 3: Restore into a separate test instance

Confirm the restore container uses a different database volume and connects to its own Postgres server. Its role needs permission to create a database.

Create a fresh database from template0, then restore into it:

(
  set -eu

  docker exec "${RESTORE_CONTAINER:?Set RESTORE_CONTAINER}" \
    createdb --no-password --template=template0 recovered

  docker exec -i "$RESTORE_CONTAINER" \
    pg_restore \
      --dbname=recovered \
      --exit-on-error \
      --no-owner \
      --no-privileges \
      --no-password \
    < "${ARCHIVE:?Set ARCHIVE}"
)

template0 gives the restore an empty starting point. --exit-on-error stops on a SQL error; the default can continue after errors. If a restore fails, start the next attempt with another fresh database. Postgres pg_restore documentation.

The two permission flags simplify this rehearsal. --no-owner skips original ownership, and --no-privileges skips grants and revokes. Restored objects belong to the restoring role. A production recovery needs the intended roles, owners, and permissions rebuilt and checked. Postgres restore options.

The separate Postgres test instance restores the archive with exit 0 and the same three ordered rows
The proof: a separate instance restores it with exit 0 and returns the same three rows in order.

Step 4: Compare rows and check the application

Query both databases:

docker exec "${SOURCE_CONTAINER:?Set SOURCE_CONTAINER}" \
  psql -X --no-password --set=ON_ERROR_STOP=1 \
  --command="SELECT id, day, orders FROM weekly_orders ORDER BY id;" \
  --command="SELECT count(*) FROM weekly_orders;"

docker exec "${RESTORE_CONTAINER:?Set RESTORE_CONTAINER}" \
  psql -X --no-password --dbname=recovered \
  --set=ON_ERROR_STOP=1 \
  --command="SELECT id, day, orders FROM weekly_orders ORDER BY id;" \
  --command="SELECT count(*) FROM weekly_orders;"

Both should return these ordered rows and a count of three:

id day orders
1 Monday 12
2 Tuesday 18
3 Wednesday 9

Compare values as well as counts. Three incorrect rows still give a count of three.

For your application database, repeat the restore in isolation. Install its required extensions and connect a separate application copy. Check login, saved records, and important queries or dashboards. Disable scheduled jobs and outbound integrations before starting that copy. Record errors and recovery time.

docker exec restore-example psql -X --no-password -U postgres --dbname=recovered --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM weekly_orders;'
 count 
-------
     3
(1 row)

The restored count is 3; compare it with the source before checking the application.

Step 5: Cover roles, files, and scheduled backups

A single pg_dump archive excludes cluster-wide roles and tablespaces. Save those separately when the recovery plan needs them:

docker exec "${SOURCE_CONTAINER:?Set SOURCE_CONTAINER}" \
  pg_dumpall --globals-only --no-password

This writes SQL to standard output. In a backup job, capture it to a private partial file and rename it only after success, as with the database dump. Globals can contain role password hashes. Restrict access and encrypt stored copies. Restore globals with psql after reviewing roles and tablespace paths for the destination. Postgres pg_dumpall documentation.

Also keep the application configuration, required encryption keys, uploaded files, and extension requirements. Store secrets separately with restricted access. The Unraid Docker backup checklist covers the rest of an app’s files.

Choose a backup interval based on how much recent data you can afford to lose. Keep multiple dated backups, copy them off the server, and alert on failed or overdue jobs. Repeat the restore check after database upgrades or backup changes. Restore only archives from trusted sources because dumps can execute code during restoration. Postgres backup security notes.

Compare backup approaches

Approach Useful when What you still need to check
pg_dump and pg_restore A small database needs a straightforward logical backup. Scheduling, retention, roles, and application recovery.
Dedicated tools such as pgBackRest You need physical backups, incremental backups, or point-in-time recovery. Repository setup, WAL archiving, retention, and restore procedures.
Database-aware backup services You want central scheduling and monitoring. Explicit Postgres support, backup method, export access, costs, and recovery into your own instance.

pgBackRest supports full, differential, and incremental backups, plus point-in-time recovery. It needs more setup than a dump script. pgBackRest user guide.

Common questions

Can I back up a running Postgres Docker volume by copying it?

An ordinary copy of changing database files is not a safe backup method. Use a logical dump, a backup taken while Postgres is shut down, or a documented physical-backup procedure with the required consistency checks. Postgres file-system backup documentation.

Do I need to stop Postgres before running pg_dump?

No. It creates a consistent database snapshot while the server runs. Check warnings and failures before accepting the backup. Postgres pg_dump documentation.

Does pg_dump include users and passwords?

It excludes cluster-wide roles. Use pg_dumpall --globals-only when you need them, and protect the resulting file because it can contain password hashes. Postgres pg_dumpall documentation.

Can a nightly dump recover changes made later that day?

No. It recovers the dump’s snapshot. Point-in-time recovery needs a physical base backup and the required sequence of archived write-ahead log files. Logical dumps cannot be replayed with WAL. Postgres point-in-time recovery documentation.

Is a successful pg_restore enough to trust a backup?

Check data and application behavior too. Compare important records, verify permissions, and test the application against the isolated restored database. Keep a record of which backup you tested and what passed.

Related: self-hosting guides · full guide collection.

../self-hosting/ every Self-hosting guide