import postgres from "postgres"; /** Admin client for migrations/backups. Accepts libpq-style socket URLs such as `postgres://user@/db?host=/tmp` (Homebrew Postgres, trust over the unix socket). */ export function adminSql() { const url = process.env.DATABASE_URL_ADMIN ?? process.env.DATABASE_URL; if (!url) throw new Error("DATABASE_URL_ADMIN missing"); const m = /^postgres(?:ql)?:\/\/([^@\/:]*)(?::[^@\/]*)?@\/([^?]+)\?host=(\/.+)$/.exec(url); if (m) return postgres({ path: m[3].endsWith(".s.PGSQL.5432") ? m[3] : `${m[3]}/.s.PGSQL.5432`, database: m[2], username: decodeURIComponent(m[1]) || process.env.USER, max: 1, onnotice: () => {} }); return postgres(url, { max: 1, onnotice: () => {} }); }