Skip to content
>_VibeUnstuck

sorry, too many clients already

Last verified:

The fix

Postgres or the pooler refused another connection. On Vercel, each warm function was opening its own pool, and the database ran out.

  1. 1

    Create one client outside the handler

    Put new PrismaClient in a module, not inside the GET or the Server Action. In development, store it on globalThis so reload does not open another pool. Prisma 7 requires the driver adapter. Calling new PrismaClient() with no adapter throws. Import the client from the folder prisma generate wrote (the Supabase guide uses ./generated/prisma/client). Do not call $disconnect() at the end of a serverless handler. The current serverless guidance says to keep the client for the next warm invocation.

    import { PrismaClient } from './generated/prisma/client'
    import { PrismaPg } from '@prisma/adapter-pg'
    
    const globalForPrisma = globalThis as unknown as { prisma?: PrismaClient }
    
    export const prisma =
      globalForPrisma.prisma ??
      new PrismaClient({
        adapter: new PrismaPg({
          connectionString: process.env.DATABASE_URL,
          max: 1,
        }),
      })
    
    if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
  2. 2

    Point the app at the pooler and keep migrations off it

    Supabase's Prisma guide uses the transaction pooler, port 6543, with pgbouncer=true, as DATABASE_URL for serverless. Migrations use DIRECT_URL on the session pooler, port 5432, or the direct host. The app URL and the migrate URL are not interchangeable. Transaction mode does not keep session state. If you are not on Supabase, use that host's pooled hostname for the app and the direct hostname for migrations.

  3. 3

    Cap the pool that this version actually uses

    If the code constructs PrismaPg, set max: 1 on that adapter for a serverless function. Supabase's troubleshooting says to start at one connection per serverless client and raise it only if you need parallel queries. If there is no adapter and the URL is passed to Prisma 6, set connection_limit=1 on that URL instead. Adding connection_limit while PrismaPg is in use does not change max. A page that fires several queries at once will run them one after another at max: 1. That is the trade for not exhausting the database.

    DATABASE_URL="postgres://prisma.[PROJECT-REF]:[PASSWORD]@aws-0-[REGION].pooler.supabase.com:6543/postgres?pgbouncer=true"
    DIRECT_URL="postgres://prisma.[PROJECT-REF]:[PASSWORD]@aws-0-[REGION].pooler.supabase.com:5432/postgres"
  4. 4

    Do not raise max_connections or switch ORMs first

    A larger database still dies if every invocation opens ten connections. Switching from Prisma to Drizzle, or the other way, does not change how many connections the driver opens. Drizzle uses the pool of the driver you passed in. node-postgres also defaults max to 10. One pool at module scope, a small max, and the host's pooler are the change. If the log is only P2024 and Postgres is not refusing connections, the function's own pool is too small or the query is slow. Then a slightly higher max is what Prisma's pool docs describe. Read which sentence you have before you edit the number.

Why it happens

Prisma's client docs show the failure as FATAL: sorry, too many clients already when too many PrismaClient instances each open a pool. One client is supposed to live for the process. A client created inside a route handler, or a new client on every hot reload, multiplies pools. On a serverless host each warm instance has its own. Supabase's Prisma troubleshooting calls the pooler version of this "Max client connections reached".

Prisma 7 connects through a driver adapter. The connection-pool docs say the pg adapter's max defaults to 10. Prisma 6 and earlier take connection_limit on the URL, defaulting to num_physical_cpus * 2 + 1. connection_limit does not cap the pg adapter. P2024 (Timed out fetching a new connection from the connection pool) is the client waiting on its own pool. That can show up before Postgres prints "too many clients". Raising max makes "too many clients" worse. An SSL error or "can't reach database server" is the pooler URL problem, not this one.

The next error you'll probably hit

Once this is fixed, the usual follow-up is self-signed certificate in certificate chain. self-signed certificate in certificate chain: the Postgres URL is missing TLS. On Vercel, use the pooled URL with sslmode=require. Leave TLS checks on.

Prompt to paste back into your AI tool

Paste this above the error.

The deployed app hits Postgres and the error is one of:
FATAL: sorry, too many clients already
Max client connections reached
P2024 Timed out fetching a new connection from the connection pool

Do not raise max_connections. Do not switch Prisma for Drizzle or the reverse. Do not add connection_limit to the URL if the client uses PrismaPg; that knob is the old engine pool. Do not call $disconnect() in the handler. Do not put the database password in a NEXT_PUBLIC_ or VITE_ variable.

1. Find every `new PrismaClient`. Leave one, created at module scope, cached on globalThis in development.
2. If the constructor uses PrismaPg, set max: 1 on that adapter for the serverless app. If it is Prisma 6 with no adapter, set connection_limit=1 on the app URL.
3. For Supabase, set DATABASE_URL to the transaction pooler (port 6543) with pgbouncer=true, and DIRECT_URL to port 5432 for migrations. Do not point both at the same URL.
4. If the error is only P2024 and the database is not refusing connections, say so and do not raise max until the singleton and the pooler URL are in place.
5. Do not rewrite queries.

Common questions

+It works on my laptop and dies after I deploy. Why?

One local process holds one pool. Vercel runs many short-lived instances, and each one that constructs a client holds its own. Ten connections times the number of warm functions is enough to hit a small Postgres or the Supabase pooler.

+I added ?connection_limit=1 and nothing changed. Why?

Prisma's pool docs map connection_limit to the v6 engine. On Prisma 7 the pg adapter uses max, and the default is 10. connection_limit in the URL does not set max. Pass max to PrismaPg.

Related errors