Prisma or Drizzle can't connect to Supabase: "Can't reach database server" or "prepared statement already exists"
The Supabase client in the browser works, but Prisma, Drizzle or postgres.js fails on Vercel or your laptop. Which connection string to copy, and the one flag each ORM needs on the transaction pooler.
Why it happens
The direct host db.[project-ref].supabase.co is IPv6 unless the project has the IPv4 add-on. Many home networks and some hosts are IPv4-only, so Prisma reports "Can't reach database server" even though the project is healthy. The shared pooler (Supavisor) is IPv4 on every plan. Its hostname is not something you can build from the region name: copy it from the dashboard Connect dialog. The username is postgres.[project-ref], not postgres. A mismatch is often "Tenant or user not found".
Serverless functions (Vercel, Supabase Edge) should use the shared pooler in transaction mode, port 6543. That mode does not support prepared statements. Prisma creates them unless the URL includes pgbouncer=true, and the error is prepared statement "s0" already exists (the name varies). Drizzle on postgres.js needs prepare: false. The dedicated pooler on Pro plans does support prepared statements from PgBouncer 1.21 on; the shared pooler in transaction mode does not.
Migrations and db pull need a session that stays open. Use the session pooler (port 5432) or a direct connection for those commands, and the transaction pooler for the running app. Characters such as @, #, ? and spaces in the database password must be percent-encoded. The database URL is a secret: it never belongs in a VITE_ or NEXT_PUBLIC_ variable.
How to fix it, step by step
- In Supabase, click Connect and copy the string for the method you need. Do not retype the host. Replace [YOUR-PASSWORD], encoding reserved characters.
- App on Vercel or another serverless host: use the transaction pooler (port 6543). On your own machine, if the direct host does not connect, use the session pooler (port 5432). It is IPv4 on every plan.
- Prisma, serverless: set DATABASE_URL to the 6543 string with ?pgbouncer=true, and pass that URL to the client. Point migrations at the 5432 session string: DIRECT_URL in older Prisma, or the datasource url in prisma.config.ts on current Prisma. Keep the pool small. The classic client reads connection_limit=1 on the URL; a driver adapter (PrismaPg) uses the pool's max instead, so set that to 1.
- Drizzle or postgres.js: postgres(process.env.DATABASE_URL, { prepare: false, max: 1, ssl: 'require' }) for a serverless function. Create that client once at module scope, not inside the request.
- Add sslmode=require to the URL if it is not already there. Supabase recommends it so the driver does not fall back to plaintext.
- Put both URLs in the host's environment variables (Vercel → Settings → Environment Variables), not in the browser bundle, then redeploy. Run a single query. If you see "Max client connections", lower the app pool before raising the database size.
Paste-ready prompt for Lovable
My server code cannot connect to Supabase Postgres. The error is:
<paste "Can't reach database server" or "prepared statement ... already exists" or "Tenant or user not found">
Please:
1. Find every PrismaClient, drizzle(), and postgres() call. The database URL must be read from an environment variable on the server only. Remove it from any VITE_ or NEXT_PUBLIC_ variable.
2. For Prisma on serverless: use the transaction pooler (port 6543) as DATABASE_URL and append pgbouncer=true. Point migrations at the session pooler (port 5432) via DIRECT_URL or prisma.config.ts datasource.url. For the classic client, start with connection_limit=1 on the pooled URL. If the project uses the PrismaPg driver adapter, set the pool max to 1 instead.
3. For Drizzle or postgres.js on the transaction pooler: pass { prepare: false, max: 1, ssl: 'require' } and create the client once at module scope.
4. Do not invent a pooler hostname. Leave a comment listing which string I must paste from Supabase → Connect (transaction vs session), and remind me to percent-encode special characters in the password.
5. Do not print the URL or password. Do not change queries besides what is required to connect.FAQ
The Table Editor works. Why does Prisma fail?
The dashboard does not use your laptop's network or your serverless function. The direct database port is IPv6-only unless you added IPv4. Use the shared pooler string from Connect.
Should I turn prepared statements off for a long-running server?
Not if you use a direct connection or session mode (port 5432). Those support prepared statements. Turn them off only for the shared pooler in transaction mode.
Still broken?
Get a free diagnosis by email within 24 hours. Fixed or refunded.
Get a free diagnosisOr get the weekly fix digest:
Free: the Vibe-Code Fix Checklist.