I kept telling people to test their backups, so it felt a bit hypocritical that I'd never restored mine.
A few weeks ago I wrote about opening my Supabase backup for the first time. I counted the rows inside the dump and compared them with the live database. That proved the file wasn't empty. It didn't prove I could actually restore it.
So I made the backup workflow restore itself, every week, into an empty Postgres. Here's how it works, and what building it showed me.
The setup
The workflow already did this every Sunday:
pg_dump "$DB_URL" -Fc --no-owner --no-privileges -n public -f backup/public.dump pg_dump "$DB_URL" -Fc --no-owner --no-privileges --data-only \ -t auth.users -t auth.identities -f backup/auth.dump Then it encrypts both files and uploads them as a GitHub Actions artifact.
For the test, GitHub Actions can start a throwaway Postgres next to the job with services. It only lives while the job runs:
services: restoretest: image: pgvector/pgvector:pg17 env: POSTGRES_PASSWORD: restoretest ports: ['5432:5432'] The test runs after the upload. If the test fails, I still have the backup.
An empty Postgres is not a Supabase project
A plain pg_restore into an empty Postgres fails right away, because a Supabase dump expects things a plain Postgres doesn't have:
- roles:
anon,authenticated,service_role(every RLS policy saysTO authenticated) -
auth.uid(), which almost every policy calls - the
extensionsschema, whereuuid_generate_v4()andgen_random_bytes()live -
supabase_functions.http_request, if you use database webhooks - the
supabase_realtimepublication
So the job creates small stand-ins first:
create role anon nologin; create role authenticated nologin; create role service_role nologin; create schema auth; create schema extensions; create extension if not exists pgcrypto with schema extensions; create extension if not exists "uuid-ossp" with schema extensions; create function auth.uid() returns uuid language sql stable as 'select null::uuid'; create publication supabase_realtime; -- (a few more, see the repo) They don't need to work like the real ones. They only need to exist so the table definitions and policies can be created.
The order matters
This is the part I had wrong in my own restore guide. I used to tell people to restore public.dump first and then the users. That fails: profiles.id has a foreign key to auth.users, and the key can't be created while auth.users is empty.
pg_restore can restore one section at a time, so the job goes in this order:
r() { pg_restore --no-owner --no-privileges --exit-on-error -d "$T" "$@"; } # 1. table definitions only r --section=pre-data backup/auth-schema.dump r --section=pre-data -L public.list backup/public.dump # 2. data: users first, then everything else r --data-only backup/auth.dump r --section=data backup/public.dump # 3. keys, indexes, triggers and RLS policies last r --section=post-data backup/auth-schema.dump r --section=post-data backup/public.dump (public.list is the dump's table of contents without the SCHEMA public line, because that schema always exists already.)
Creating triggers last also means a "create a profile on signup" trigger can't fire while the users are being loaded and try to create every profile a second time.
What building it showed me
To get the restore order right, I listed every trigger in my dumps. My two signup triggers weren't there. Not in any of my backup files.
One creates a profile for every new user, the other fills in a temporary username for Google sign-ins. It makes sense once you see where they live:
create trigger on_auth_user_created after insert on auth.users for each row execute function public.handle_new_user(); The functions are in public, so they were in the backup. The triggers are on auth.users, and I only dumped the data of auth.users. If I had restored this backup into a new project, everything would have looked fine, and every new signup after that would have had no profile.
The fix is one more dump, with the definitions of the two auth tables (triggers included):
pg_dump "$DB_URL" -Fc --no-owner --no-privileges --schema-only \ -t auth.users -t auth.identities -f backup/auth-schema.dump When restoring into a real new Supabase project, you only take the triggers from this file, since the project already has its own auth.users.
The check
After the restore, the job counts the rows of every table in the restored database and compares them with the rows inside the dump, then writes a table to the run's Summary page:
| table | rows in backup | rows after restore |
|---|---|---|
| auth.users | 6 | 6 |
| public.profiles | 6 | 6 |
| public.user_films | 562 | 562 |
| public.film_meta | 920 | 920 |
That's from my own app: 18 tables, all matching, plus 16 tables with RLS, 23 policies, 10 functions and 5 triggers. If any table is off, the job turns red.
What it doesn't check
- Storage files.
pg_dumponly copies the database. Uploaded images need their own backup. - Whether the data is correct. It counts rows, it doesn't compare every value.
- Login. The stand-in
authschema has no real auth server. Password hashes are restored, but the test doesn't sign anyone in. - Every extension. If your tables use an extension that isn't in the test image, the restore fails and the job turns red. That's annoying, but it's the right kind of failure: it means a real restore would need that extension too.
The workflow is free and runs in your own GitHub: supabase-encrypted-backup. It's one file to copy plus two secrets.
If you'd rather have someone set it up and check it for you, I do that here: https://www.fiverr.com/kushoevski
I wrote this post with help from AI tools. The workflow, the test runs and the numbers are real.
