Fix “remaining connection slots are reserved” on Supabase from serverless functions
Updated
remaining connection slots are reserved for non-replication superuser connectionsEach serverless or edge invocation opens its own direct Postgres connection, and the database runs out of slots. Connect through Supabase’s pooler in transaction mode on port 6543, disable prepared statements in your driver, and keep the direct connection only for migrations.
Why it happens
A direct connection suits a long-running server that keeps a few connections open. Vercel functions, Supabase Edge Functions and similar platforms start many short-lived instances, and each one connects separately. Fine with ten users, not with a launch day. The same message can appear as sorry, too many clients already.
The fix
- In the Supabase dashboard open Connect and copy the Transaction pooler string. It uses port
6543. - Use it as
DATABASE_URLfor the app. - Turn off prepared statements, which the shared pooler doesn’t support in transaction mode:
// postgres.js and Drizzle const sql = postgres(process.env.DATABASE_URL!, { prepare: false }) // Prisma: add to the connection string // ?pgbouncer=true - Keep the direct connection string for migrations and
pg_dump, for example asDIRECT_URL.
Check that it worked
Run a short load test, for example 50 parallel requests to a page that queries the database, and watch the connections chart in the dashboard. It should stay flat instead of climbing with each request.
If your app talks to Supabase only through supabase-js, it uses the HTTP API rather than direct connections, and this error comes from another part of the stack: an ORM, a cron job or a script.