fix: use percent-encoded DATABASE_URL instead of split DB_PASSWORD trick
Drizzle ORM mutates client.options (parsers/serializers) after the postgres client is created, which causes the separately-passed password option to be lost on the actual connection attempt. Root cause confirmed on VPS: raw postgres.js query succeeded while drizzle.execute() failed with auth error. Fix: encode the password directly in DATABASE_URL (%23 = #, %5D = ], %3D = =). postgres.js decodes percent-encoding correctly. No separate DB_PASSWORD env var needed in the app container anymore. DB_PASSWORD is still used by the Postgres container (POSTGRES_PASSWORD). Coolify env var to set: DATABASE_URL=postgres://wc:<encoded-pass>@db:5432/worldcup Also adds resolver-level isMissingTable() guards so the app returns empty results instead of GraphQL errors on a fresh deploy before sync runs. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+6
-8
@@ -2,14 +2,12 @@ import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
import postgres from 'postgres'
|
||||
import * as schema from './schema'
|
||||
|
||||
const connectionString = process.env.DATABASE_URL ?? 'postgres://wc:wc@localhost:5432/worldcup'
|
||||
|
||||
// DB_PASSWORD is passed separately to avoid URL-encoding issues with special chars
|
||||
// (e.g. #, ], = in passwords break URL parsing). Falls back to password in DATABASE_URL for local dev.
|
||||
const client = postgres(connectionString, {
|
||||
max: 10,
|
||||
...(process.env.DB_PASSWORD ? { password: process.env.DB_PASSWORD } : {}),
|
||||
})
|
||||
// Passwords with special chars (#, ], =) must be percent-encoded in DATABASE_URL.
|
||||
// postgres.js decodes them correctly (e.g. %23 → #). No separate DB_PASSWORD needed.
|
||||
const client = postgres(
|
||||
process.env.DATABASE_URL ?? 'postgres://wc:wc@localhost:5432/worldcup',
|
||||
{ max: 10 }
|
||||
)
|
||||
export const db = drizzle(client, { schema })
|
||||
|
||||
export * from './schema'
|
||||
|
||||
Reference in New Issue
Block a user