Skip to content
Mementropy
RecipesHomelab recipesReference

Verifying a Postgres dump by restoring it into a scratch database

Restore each dump into a throwaway database and compare table counts.

By Ryan Hammang

Catching empty or broken Postgres dumps before you need them.

  • kubectl exec access to the Postgres pod
  1. Dump in custom format:
Terminal window
kubectl exec -n <ns> <pg-pod> -- pg_dump -Fc -U <user> -d <db> > db.dump
  1. Make a scratch database:
Terminal window
S=restore_verify_$(date +%s)
kubectl exec -n <ns> <pg-pod> -- createdb -U <user> $S
  1. Restore into it:
Terminal window
kubectl exec -i -n <ns> <pg-pod> -- \
pg_restore --no-owner --no-privileges -U <user> -d $S < db.dump
  1. Count public tables in both:
Terminal window
Q="SELECT count(*) FROM information_schema.tables WHERE table_schema='public';"
kubectl exec -n <ns> <pg-pod> -- psql -U <user> -d <db> -tAc "$Q"
kubectl exec -n <ns> <pg-pod> -- psql -U <user> -d $S -tAc "$Q"
  1. Drop the scratch database:
Terminal window
kubectl exec -n <ns> <pg-pod> -- dropdb -U <user> $S

The two counts match. Counts seen in this homelab: Infisical 752, Gitea 116, LiteLLM 91, Kaneo 45, Hindsight 24.

  • Don’t assume table names. Infisical renames knex_migrations to infisical_migrations.
  • For a migration, compare row counts on the important tables too.