Fix “remaining connection slots are reserved” on Supabase from serverless functions

Updated

Error messageremaining connection slots are reserved for non-replication superuser connections

Each 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

  1. In the Supabase dashboard open Connect and copy the Transaction pooler string. It uses port 6543.
  2. Use it as DATABASE_URL for the app.
  3. 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
  4. Keep the direct connection string for migrations and pg_dump, for example as DIRECT_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.

Sources