Skip to content

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

Supa2Selfhost

Guided Node.js CLI for migrating a hosted Supabase project to a self-hosted Supabase deployment running behind Coolify.

Supa2Selfhost automates the parts that can be handled safely from your machine, then generates a clear report for the manual cutover work that still belongs in Coolify, OAuth dashboards, and your application deployment.

What It Does

  • Collects migration credentials through an interactive CLI flow.
  • Runs preflight checks for Node.js, the packaged Postgres client, Supabase Auth health, and old/new Postgres connectivity.
  • Dumps the hosted Supabase database into roles.sql, schema.sql, and data.sql using bundled Node dependencies.
  • Restores the dump into the self-hosted database in a single transaction.
  • Verifies key database objects such as Auth users, identities, storage metadata, public tables, policies, functions, triggers, extensions, and Realtime publication tables.
  • Copies Supabase Storage files by reading storage.objects, downloading each hosted object through the Storage API, and uploading the physical object directly to MinIO/S3 under the storage-api v1.22 version-suffixed key (bucket/name/version, or the base key for null versions).
  • Writes a redacted checkpoint and migration report so failed or interrupted runs can be resumed without storing secrets.
  • Generates manual tasks for Coolify env vars, OAuth callback URLs, app env vars, Edge Functions, and final production cutover.

What It Does Not Do

Supa2Selfhost intentionally does not:

  • Put your app into maintenance mode automatically.
  • Edit Coolify environment variables.
  • Update OAuth provider dashboards.
  • Deploy your application.
  • Delete or disable the old Supabase project.
  • Store service-role keys, DB passwords, or full DB connection strings in checkpoints.

Requirements

  • Node.js >=20
  • npm
  • Network access to both old hosted Supabase and new self-hosted Supabase
  • A deployed self-hosted Supabase stack in Coolify

Before running a migration, verify your new self-hosted Supabase Auth endpoint works:

curl -i https://YOUR_SUPABASE_DOMAIN/auth/v1/health \
  -H "apikey: YOUR_ANON_KEY"

Install And Build

From this repository:

npm install
npm run build

Run the CLI locally:

node dist/index.js --help

Or link it for local development:

npm link
supa2selfhost --help

Commands

Run the full guided migration:

supa2selfhost migrate

Run the Docker-local one-click migration flow:

supa2selfhost auto --db-container YOUR_SUPABASE_DB_CONTAINER --yes

auto is the recommended path for Coolify/self-hosted Supabase when the machine running the CLI can access Docker. It uses a version-matched postgres:<major> Docker image for pg_dump, backs up the target DB before restore, validates dump completeness, restores inside the target DB container as supabase_admin, applies the required service_role grants for Storage API runtime access, smoke-tests Storage, copies objects to MinIO/S3 using version-suffixed keys, and writes an auto-migration-report.txt.

For production hardening, auto treats preflight failures as hard stops. It runs the migration-lessons gates before restore: DB_ENC_KEY length, carried-forward JWT/key consistency, GoTrue image/schema alignment, absolute compose mounts, compose/env corruption checks, service command/env checks, extension availability, Vault/pgsodium usage, storage sizing/headroom, live_update_cursors, Realtime tenant Host alignment, and Kong consumer JWT validation. If the script cannot prove an environment-specific fact from the supplied inputs, it reports that as an assumption and fails the gate instead of claiming it is verified.

Optional auto flags:

--storage-url <url>                 Use an internal Storage/Supabase API URL for smoke tests
--keep-dump                         Keep docker-dump.sql in the artifact directory
--run <id>                          Use or resume a specific run ID
--compose <path>                    docker-compose.yml to lint for hardening gates
--env-file <path>                   .env to lint for redaction/truncated secrets
--jwt-secret <secret>               Prod JWT_SECRET carried forward from hosted
--db-enc-key <key>                  16-byte Realtime DB_ENC_KEY
--gotrue-image <image>              Pinned GoTrue image to compare migrations
--realtime-host <host>              Kong-forwarded Realtime Host subdomain
--storage-disk-path <path>          Path whose free space backs Storage migration
--live-cursor-unique-cols <cols>    Comma-separated live_update_cursors unique columns
--s3-endpoint <url>                 MinIO/S3 endpoint for direct object upload
--s3-access-key-id <key>            MinIO/S3 access key id
--s3-secret-access-key <secret>     MinIO/S3 secret access key
--s3-bucket <bucket>                MinIO/S3 bucket used by storage-api, default stub
--s3-region <region>                S3 signing region, default us-east-1

Each option also has an environment-variable equivalent prefixed with SUPA2SELFHOST_, for example SUPA2SELFHOST_JWT_SECRET, SUPA2SELFHOST_DB_ENC_KEY, SUPA2SELFHOST_COMPOSE, and SUPA2SELFHOST_S3_ENDPOINT.

Run only preflight checks:

supa2selfhost preflight

Advanced/manual: create a compressed dump archive from the old hosted database:

supa2selfhost dump --old-db-url "postgresql://..." --out ./old-project.supa2selfhost-dump.gz

Advanced/manual: restore a dump archive into the new self-hosted database:

supa2selfhost restore --archive ./old-project.supa2selfhost-dump.gz --new-db-url "postgresql://..."

Resume a previous run:

supa2selfhost resume --run <RUN_ID>

Checkpoints are stored in .supa2selfhost/. Migration artifacts are stored in supa2selfhost-artifacts/<RUN_ID>/.

Credentials You Need

The CLI will prompt for:

  • Old hosted Supabase URL
  • Old anon key
  • Old service-role key
  • Old Postgres connection string
  • New self-hosted Supabase URL
  • New anon key
  • New service-role key
  • New self-hosted Postgres connection string
  • App domain
  • Supabase public domain
  • Whether OAuth is used
  • Whether SMTP is used

For the new self-hosted Postgres connection string, use the actual Postgres host and exposed port. Self-hosted Supabase does not publicly expose Postgres by default, so do not reuse the public Supabase API domain unless that hostname is explicitly routed to Postgres on port 5432.

Secrets are kept in memory only. On resume, the CLI asks for secrets again.

Migration Flow

  1. Collect credentials interactively.

  2. Run preflight checks.

  3. Dump old database with the packaged Postgres client.

  4. Restore into the new database in a single transaction.

  5. Verify database restore.

  6. Copy Storage buckets and objects.

  7. Generate the migration report and manual cutover checklist.

Safety Notes

  • Run this first against a test self-hosted Supabase instance before production cutover.
  • Put your application in maintenance mode before the production database dump.
  • Stop background jobs, cron tasks, webhooks, and workers that write to the old project.
  • Keep the old Supabase project available for rollback until the new deployment is fully verified.
  • Review supa2selfhost-artifacts/<RUN_ID>/migration-report.txt before final cutover.

Manual Cutover Checklist

After automated steps complete, update the remaining external systems:

  • Coolify Supabase/Auth environment variables:
    • GOTRUE_SITE_URL
    • GOTRUE_URI_ALLOW_LIST
    • API_EXTERNAL_URL
    • SUPABASE_PUBLIC_URL
    • SMTP env vars if needed
    • OAuth provider env vars if needed
  • OAuth provider callback URLs:
https://YOUR_SUPABASE_DOMAIN/auth/v1/callback
  • Frontend env vars:
NEXT_PUBLIC_SUPABASE_URL=https://YOUR_SUPABASE_DOMAIN
NEXT_PUBLIC_SUPABASE_ANON_KEY=NEW_ANON_KEY
  • Backend/job env vars:
SUPABASE_URL=https://YOUR_SUPABASE_DOMAIN
SUPABASE_SERVICE_ROLE_KEY=NEW_SERVICE_ROLE_KEY

Then redeploy your app, test auth/storage/realtime/functions, monitor logs, and only disable maintenance mode once everything checks out.

Development

Run type checks:

npm run lint

Run tests:

npm test

Build:

npm run build

Current Limitations

  • The production auto storage path requires direct MinIO/S3 credentials so it can place files at the exact version-suffixed keys expected by restored storage.objects rows.
  • Large storage buckets may take time because objects are downloaded and uploaded through the CLI process.
  • The project has unit tests, but full production confidence still requires integration testing against disposable Supabase/Postgres instances.
  • Section 0 dump/restore behavior is implemented as gates and backup steps, but it is not proven against your actual hosted project or target container until you run it there. Treat dump completeness, extension compatibility, Vault/pgsodium counts, and encrypted Realtime blob handling as environment facts that must be confirmed by a real preflight run.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages