Database workflow: local Supabase to v2
Owner: @zuki · Written by the Backend Engineer · Last reviewed: 2026-10-12 · Conforms to the decisions through D32
How the v2 database changes: every change is built and tested on the local Supabase stack, then reaches staging by a rebuild from main, and production once, in cutover week. Nobody edits a remote database by hand. The Backend Engineer keeps this guide; the decisions and the rules outrank it (how we work).
It moved here on 2026-10-12 from the Google doc [Guide] Database workflow: local Supabase to v2 (#51, D28). On the way, testing became pgTAP with no reset afterwards (D12), the keys became the publishable and secret keys (D13), and the one-time setup became pointers to the steps that do it.
Before you start
Section titled “Before you start”The rule of this workflow. Migration files are the only way the v2 schema changes (D4). Until cutover they are edited in place, so run pnpm supabase db reset after every pull (D11).
- Every change runs on the local stack first. Staging is rebuilt from main; production gets nothing before cutover week, and only zuki pushes to it (D11).
- Nobody edits tables, policies or functions in a Supabase Dashboard. If it happens anyway, capture the change into a file (troubleshooting).
- What to follow when you write the SQL is in The database: adding to the schema.
You need:
- Docker (Docker Desktop or a compatible engine).
- Node 24 and pnpm 12, as the repository pins them. Every command here is pnpm.
- The Supabase CLI, a devDependency of the repository, run as
pnpm supabase <command>(TL-M0-16). - A psql client of the same major version as the database, 17.
Secrets:
- Run
pnpm supabase loginonce; the CLI keeps your access token. - Keep each database password in your password manager. Type it when the CLI asks, or set SUPABASE_DB_PASSWORD for that shell only.
- Never put a password or a key in a file, the repository, a doc or a chat. Steps show them only as placeholders such as <STAGING_DB_PASSWORD>.
- The keys are the publishable key and the secret key (D13). anon, authenticated and service_role appear only as Postgres role names.
Part A · One-time setup
Section titled “Part A · One-time setup”The timeline and the backend runbook do this; this part only says where.
| What | Done by |
|---|---|
| The CLI and an empty local project | TL-M0-16 |
| config.toml: Postgres 17, sign-up off, the site URL, the services M0 doesn't use switched off | BE-M0-10 |
| The local signing key, so getClaims() verifies tokens locally (D13) | BE-M0-11 and BE-M0-32 |
| The staging project | TL-M0-20, #21 |
| Link the CLI to staging, the first push, the seed and the E2E logins | Backend pack F, #22 |
On a fresh clone, make the signing key once before the first start; it is git-ignored, so each machine has its own:
pnpm db:signing-keypnpm supabase startpnpm supabase start runs Postgres, Auth, Storage, the API and Studio in Docker, applies every migration and seed.sql, and prints the local URLs and keys. If a migration fails, the start stops at that file and shows the error.
Settings worth knowing in supabase/config.toml:
[db] major_version = 17must equal the v2 projects'show server_version;.[api] schemasstays public and graphql_public. Never add app or a migration schema.[auth] enable_signup = false. Staff are invited (OQ2); on staging and production, set it in the Dashboard.- Storage buckets are made in SQL by supabase/migrations/20261006001300_storage.sql, not in config.toml, so there is one source.
Part B · Every change
Section titled “Part B · Every change”B1. Start the stack if it isn't running:
pnpm supabase startB2. Write the change. Until cutover, edit the migration file that owns the area. From cutover on, add a new file:
pnpm supabase migration new <short_name>First the rule in apps/docs, then its pgTAP test, then the SQL (D27). If you changed the access spec in tools/db/gen_rls.py or the seed spec in tools/db/gen_seed.py, regenerate their migrations:
python3 tools/db/gen_rls.pypython3 tools/db/gen_seed.pyB3. Rebuild from nothing. Local only: this drops the local database, applies every migration, then seed.sql.
pnpm supabase db resetDo this after every change and every pull. It proves the whole chain works from an empty database, which is what staging does every night.
B4. Test.
pnpm supabase test db- Every file in supabase/tests runs in its own transaction and is rolled back, so no reset is needed afterwards (D12). How to write one is in the testing guide, moving under #55.
pnpm supabase db lintfinds typing errors in functions.pnpm docs:ruleschecks that every test cites a rule that exists.- By hand: open Studio, or point apps/web at the local API URL and publishable key and sign in as an E2E login.
pnpm db:e2e-userscreates them and writes their emails and password to the git-ignored .env.e2e.
B5. If you changed something in Studio while trying things out, turn it into a file, read the SQL it wrote, then go back to B3:
pnpm supabase db diff -f <short_name>B6. Regenerate what is generated from the schema, then commit it with the migration and its tests:
pnpm db:typespnpm docs:referenceThe CI database job (TL-M0-18) will fail when either is stale. Who regenerates the types after a merge is OQ21.
B7. Staging changes by being rebuilt from main, never by db push: edited migration files no longer match staging's history (D11). Until the rehearsal job exists, zuki rebuilds it by hand from a runbook step. After a rebuild, check one query from your change on staging, or sign in with an E2E login.
B8. Production gets its first push in cutover week, after the migration files are frozen into a baseline (D11). zuki pushes it by hand, and that step stays manual for good (the automation ladder). Never --include-seed, never db reset --linked, and only what has already passed on staging.
Part C · Test the v1 to v2 migration locally
Section titled “Part C · Test the v1 to v2 migration locally”From the data phase, once tools/migration exists. Data phase row 1 is this part, written as a step pack.
C1 to C3. Take a copy of v1, restore it, and point local v2 at it: BE-DP-05 to BE-DP-07, pack R1 of the data phase. In short, and why each part is the way it is:
- The dump reads v1 through
v2_reader, the read-only login of TL-DP-01, with--schema=public. Never an admin password, and never a CLI linked to v1. It runs from Docker'spostgres:17image, because the Mac has no psql or pg_dump. - The dump holds personal data (NIK, phones). It lives in ../opleet-private, outside the repository, and you delete it when you are done (D15).
- The copy is a container,
v1copy, on the local stack's networksupabase_network_opleetand on no port of the Mac. Port 54329, which this guide used to publish it on, is the local stack's pooler. - Before the restore,
tools/migration/v1-copy/prep.sqlgives the copy stand-ins for what v1's tables lean on:extensions.uuid_generate_v4(),auth.uid()and the API roles. Without them, those tables fail to create, and pg_restore only warns and carries on. The step then proves no table is missing. - Local v2 reads views, not v1's tables.
export.sqlmakesv1_exportin the copy (a view for each table, and for v1'sv_rental_fee_audit, which the rentals step reads), where v1's own types (enums, domains) are text, because local v2 doesn't have them, or has different types with the same names.link-v1copy.sqlimports those views intov1_livethrough postgres_fdw.db resetremovesv1_live, so run BE-DP-07 again after each reset.
C4. Run the pipeline, the same calls as a night on staging (how the data moves):
- The snapshot.
- Unmapped values, which must be empty.
- Each domain step, in order.
- Reconcile, which must return no unexplained rows (D18).
Fix what it finds, then pnpm supabase db reset, run C3 again and rerun.
C5. Photos. Locally, test the copy script on a handful of files only. The full copy runs on staging.
When you are done: docker rm -f v1copy, and delete the dump (BE-DP-08's Undo).
Part D · Rules that keep v2 safe
Section titled “Part D · Rules that keep v2 safe”- From cutover on, a migration file is never edited; fix it forward with a new file (D11).
db resetruns locally.db reset --linkedwipes the linked project: only ever on a staging project you mean to wipe, never on production.--include-seedonly on local and staging. The seed is the sealed E2E organization.- No schema edits in a Dashboard.
- Run
cat supabase/.temp/project-refbefore every push, to see which project is linked. - supabase/.temp/, the signing key, .env files and dumps stay out of git.
Troubleshooting
Section titled “Troubleshooting”| Problem | Fix |
|---|---|
supabase start stops on a migration |
Read the file and line in the error. Before cutover, fix that file (D11); after cutover, add a new one. Then db reset. |
supabase start fails on the signing key |
Run pnpm db:signing-key, then start again. |
db push reports remote migrations not found locally |
Before cutover this is expected once files were edited: staging is rebuilt, not pushed (D11). Otherwise someone changed the remote outside this workflow: run pnpm supabase migration list, capture the change with db diff --linked -f <name>, read it and commit it. Use migration repair only when you are sure the history table itself is wrong; never edit it by hand. |
db push refuses an older timestamp |
A file is older than the last one applied. Create it again with migration new. |
| Password or auth errors on link or push | Set SUPABASE_DB_PASSWORD for the shell, or reset the password in the Dashboard. |
| Can't connect to a Supabase database from your network | Use the session pooler connection string (IPv4) instead of the direct one. |
| Extension or version errors that appear only remotely | [db] major_version in config.toml must equal the project's show server_version;. |
operator does not exist: uuid = uuid[] in a new policy |
Add the ::uuid[] cast: = any ((select app.pools_with_any(array[…]))::uuid[]). |
supabase test db fails with "no plan" |
A test file lacks select plan(n); or select * from finish();. |
tables without RLS or missing from tools/db/gen_rls.py |
Add the new table to the spec in tools/db/gen_rls.py, regenerate, reset. |
| Ports already in use | pnpm supabase stop, or change the ports in config.toml. |