Files
loki/docs/infrastructure/postgres-portability.md
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

6.1 KiB

Postgres portability (studio-wide)


created_date: 2026-05-22 last_modified_date: 2026-06-16 last_modified_summary: Reflect the completed Hetzner self-hosted reality; drop the dead Oracle/Vercel cutover machinery; keep the vendor-neutral env-var pattern.

Goal: switching Postgres host later is a config + dump/restore job, not a code rewrite.

Long-form narrative: The Database Kill Switch.

Applies to Loki first; copy the same env pattern to other Drizzle/Prisma apps in ~/dev.

Where things stand

Loki's Postgres is self-hosted on the Hetzner bitbaum box (Postgres 17 on the host, one role/database per app). The app reads DATABASE_URL; there is no managed-DB vendor in the loop. The one-time migration off Neon (Loki) and managed Supabase (OrangeCat) completed 2026-06-12 — see docs/infrastructure/hetzner-migration.md for the box layout and history. The vendor-neutral env pattern below is what made that move a config change rather than a rewrite, and is what keeps the door open to moving hosts again later.

Principles

  1. Standard Postgres only — no vendor-specific SQL, extensions, or SDKs in app code.
  2. Two URLs in production — direct for migrations/LISTEN; pooled for runtime queries (pooled may equal direct when no pooler is in front).
  3. Local dev never hits prod — .env.local → Docker Postgres on localhost.
  4. One database per project on the shared host — not one managed project per app.
  5. Self-hosted Supabase stays — OrangeCat/printcraft use auth, RLS, storage via the self-hosted Supabase stack at supabase.orangecat.ch; do not force them onto plain Postgres without a rewrite.

Environment variables (SSOT)

Variable Purpose Who sets it
DATABASE_URL Direct session (:5432) — migrations, drizzle-kit push, LISTEN/NOTIFY app env on the box + local dev
DATABASE_POOL_URL Pooled session (PgBouncer :6432) — app runtime queries production only (optional; falls back to direct)

Local dev: set DATABASE_URL only. Pool URL falls back to the same value.

The legacy NEON_DATABASE_URL / NEON_DATABASE_URL_DIRECT fallbacks were removed — only the neutral DATABASE_URL / DATABASE_POOL_URL names are read now.

Implementation: src/lib/db-url.ts — getDatabasePoolUrl(), getDatabaseDirectUrl().

Dev vs production

┌─────────────────────────────────────────────────────────────┐
│ Laptop: docker compose up db                                │
│   DATABASE_URL=postgresql://loki:changeme@localhost:5432/loki
│   (never the prod URL in .env.local)                        │
└─────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────┐
│ Production (self-hosted Next.js on the Hetzner box)         │
│   DATABASE_URL=postgresql://user:pass@host:5432/loki     │
│   DATABASE_POOL_URL=postgresql://user:pass@host:6432/loki (only if PgBouncer is in front)
└─────────────────────────────────────────────────────────────┘

App code is unchanged when the host changes — only the env URLs move.

Self-hosted target layout

One VM, Postgres 17, optional PgBouncer (this is the current bitbaum setup):

# On the VM (example)
createdb loki
createdb kivvi
createdb revampit
# … one DB per project

# PgBouncer (optional) listens on 6432 → forwards to localhost:5432

Point each app at its own database name on the same host via its DATABASE_URL.

Migration runbook (when you switch hosts)

Full database (schema + data)

# 1. Backup source
DATABASE_URL="$OLD_DIRECT_URL" ./scripts/db/dump.sh loki-pre-migrate.sql.gz

# 2. Create empty DB on new host, push schema
DATABASE_URL="$NEW_DIRECT_URL" pnpm run db:push

# 3. Restore
DATABASE_URL="$NEW_DIRECT_URL" ./scripts/db/restore.sh loki-pre-migrate.sql.gz

# 4. Set DATABASE_URL (+ DATABASE_POOL_URL) in the app env on the box, redeploy
#    (scripts/deploy-hetzner.sh)
# 5. Smoke test sign-in + one CRUD path + Control runtime push

User data only (schema already on target)

SOURCE_DATABASE_URL="$OLD" TARGET_DATABASE_URL="$NEW" TARGET_USER_ID="<uuid>" \
  ./scripts/db/sync-user-data.sh

Cutover checklist

  • DATABASE_URL (direct) and DATABASE_POOL_URL (pool, if used) set in the app env
  • Local .env.local still points at Docker — not new prod
  • pnpm run db:push succeeds against new direct URL
  • Auth sign-in works
  • Control SSE / runner push updates runtime_snapshots
  • Decommission old host after 7 days (keep a cold dump for 30)

Loki scripts

Script Purpose
pnpm run test:db-url Assert URL resolution logic
scripts/db/dump.sh (pnpm run db:dump) Portable pg_dump backup
scripts/db/restore.sh (pnpm run db:restore) Restore into target DB
scripts/db/restore-to-target.sh Restore + verify row counts against a manifest
scripts/db/sync-user-data.sh Copy user-owned rows between DBs

What we are not doing

  • Self-hosted-Supabase-native apps (OrangeCat) stay on Supabase — not migrated to plain Postgres.

Other projects in ~/dev

Pattern Action
Drizzle/Prisma + plain Postgres Use DATABASE_URL (+ pool if needed); local Docker only
Supabase-native (orangecat, printcraft) Keep on self-hosted Supabase; skip this doc for DB
Local-only (reparaturbonus, datacat) Already portable — use the same dump/restore pattern

Extract src/lib/db-url.ts into a shared snippet or copy per repo when you migrate another app.