Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0009

Supabase as hosted Postgres for staging

Trạng thái
accepted
Ngày
2026-09-21
Người quyết định
Alex

Context

Local development runs Postgres in Docker (ADR-0006). A deployed environment needs a managed Postgres with vector and pg_trgm, backups and a dashboard the whole team can reach, at no cost for a capstone. Supabase project smart-learning-ai (ref lqbnldwxodvhmevfunkw, region ap-southeast-2) provides that.

Decision

  • Supabase is only a Postgres host. The app does not use Supabase Auth, Storage, Realtime or the Data API. NestJS stays the only way into the data (ADR-0002, ADR-0007).
  • Prisma owns the schema. Migrations reach Supabase through prisma migrate deploy, never through the Supabase dashboard or the MCP apply_migration tool. Otherwise the two migration histories drift apart.
  • Extensions are created by hand, in the extensions schema, matching infra/db/init/001-extensions.sql: vector and pg_trgm.
  • The Data API is closed. anon and authenticated have no privileges on public, including default privileges for tables created later. RLS is enabled with no policies (deny-all). The app connects as postgres, which owns the tables and is not affected.
  • The connection string lives in .env.local as STAGING_DATABASE_URL. It is never committed.

Alternatives considered

  • Postgres in the compose stack on a VPS. Backups and upgrades become our job.
  • Neon. Comparable, but the team already works in the Supabase dashboard.

Consequences

  • To migrate staging: DATABASE_URL="$STAGING_DATABASE_URL" pnpm --filter @smartlearn/database exec prisma migrate deploy.
  • STAGING_DATABASE_URL is the Session pooler string (postgres.<ref>@…pooler.supabase.com:5432), because the direct host db.<ref>.supabase.co is IPv6-only and many CI runners lack IPv6. Never use the Transaction pooler (port 6543) for migrations.
  • Every migration that adds a table must also ENABLE ROW LEVEL SECURITY on it, or the Supabase security advisor reports it. The revoked grants already keep it unreachable.
  • The advisor's rls_enabled_no_policy notice is expected and stays.