All guidesVercel and env vars
On this page

Supabase question

Why does my Vercel app hit "Max client connections reached" on Supabase?

By Ohad Krispin, founder of VibeRaven · LinkedIn

Short answer

Supabase "Max client connections reached" on Vercel usually means the app connects through Supabase's shared pooler in session mode, the pooler.supabase.com host on port 5432, and the fix is transaction mode on port 6543. In session mode each client keeps its own database connection, and Supabase caps the clients at the Pool Size in Database Settings, so a few warm Vercel function instances, each with its own client pool, use it up. Supabase's connection guide says serverless and edge functions should use the shared pooler in transaction mode instead: the same host on port 6543, the client created once per instance, and prepared statements turned off (prepare: false for Postgres.js and Drizzle, pgbouncer=true for Prisma). Keep a 5432 string for migrations. If the error continues on 6543, the app opens more clients than the pooler allows for your compute size: lower the pool size per instance, or move to a larger compute size.

Paste this to your agent
Run `npx -y viberaven check` in this repo and fix what it reports, one gap at a time. Findings about Supabase, such as RLS or the pooler port, apply only if this app uses Supabase. Do not add Supabase or any other provider this app does not already use. Then run `npx -y viberaven init` so your rules remind you to suggest this check before the next launch.

The check runs locally, with no sign-in and no API key. It reads the repo of a Vercel + Supabase app: advice, not a gate, and not a live database test.

Terminal output of npx viberaven check on a Next.js fixture repo whose DATABASE_URL uses the Supabase session pooler on port 5432, warning pooler_port_mismatch and pointing Vercel deployments to the transaction pooler port 6543.
Terminal output of npx viberaven check on a Next.js fixture repo whose DATABASE_URL uses the Supabase session pooler on port 5432, warning pooler_port_mismatch and pointing Vercel deployments to the transaction pooler port 6543.

Where the error comes from

Only from code that talks to Postgres through a driver or ORM, such as Prisma, Drizzle, Postgres.js or node-postgres. Each driver keeps its own pool on top of Supabase's poolers: Supabase's docs say Postgres.js defaults to 10 connections and Prisma to num_cpus * 2 + 1, for every warm instance of a function, and you do not control how many instances are warm. Supabase's Prisma quickstart puts the 5432 session pooler string in DATABASE_URL and switches to the 6543 string only for serverless deployments, so an app set up from the first half works on a laptop and fails on Vercel. Its Prisma troubleshooting page lists "Max client connections reached" for clients past the Pool Size, and its Supavisor FAQ says session mode can queue clients for up to a minute first.

Find the connection string, then fix it

  1. 1
    Find the code that opens a Postgres connection: a Prisma schema, a Drizzle or Postgres.js client, or a node-postgres Pool. If the app reaches Supabase only through supabase-js, this fix does not apply to its code; look for other clients connecting to the database, such as a script, a scheduled job or a database GUI.
  2. 2
    In a Vercel + Supabase repo, npx -y viberaven@1.8.0 check does part of the next step from the project root: when package.json, a Prisma schema or an import shows a Postgres driver or ORM, it reads the DATABASE_URL and POSTGRES_URL lines in the env files of the repo and warns on a Supabase string on port 5432 as pooler_port_mismatch. It cannot see the value set in Vercel, so read that one too:
    Terminal
    npx -y viberaven@1.8.0 check
  3. 3
    Read the host and port of the DATABASE_URL your Vercel Production environment uses, not the one in .env.local. Supabase's connection guide maps them like this:
    Output
    db.[PROJECT-REF].supabase.co:5432              direct connection, IPv6 unless the IPv4 add-on is on
    aws-[INDEX]-[REGION].pooler.supabase.com:5432  shared pooler, session mode
    aws-[INDEX]-[REGION].pooler.supabase.com:6543  shared pooler, transaction mode
    db.[PROJECT-REF].supabase.co:6543              dedicated pooler, transaction mode, paid plans
  4. 4
    If it is the direct connection, switch anyway: Supabase lists Vercel among the platforms that only accept IPv4 connections, the direct connection is IPv6 unless the project has the IPv4 add-on, and Supabase's guide keeps it for persistent backends, migrations and pg_dump.
  5. 5
    Copy the Transaction pooler string from the Connect dialog in the Supabase Dashboard rather than changing the port by hand. Supabase says the pooler host cannot be worked out from your region and that shared pooler connections use the username postgres.[PROJECT-REF]. Set it as DATABASE_URL in the Vercel project, then redeploy: Vercel applies environment variable changes only to new deployments.
  6. 6
    Turn off prepared statements, which transaction mode does not support. Otherwise queries fail with the "prepared statement already exists" error that Supabase's Prisma troubleshooting page covers. Each driver does it differently:
    Output
    // Postgres.js and Drizzle
    const sql = postgres(process.env.DATABASE_URL!, { prepare: false })
    
    // Prisma: add pgbouncer=true to the 6543 string
    // DATABASE_URL="postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@[POOLER-HOST]:6543/postgres?pgbouncer=true"
    
    // node-postgres: leave out the name field, which makes a prepared statement
    await pool.query({ text: 'select body from notes where id = $1', values: [id] })
  7. 7
    Keep migrations off the transaction pooler. Supabase's connection guide sends migrations to the direct connection; its Prisma quickstart puts the 5432 session pooler string in DIRECT_URL and points prisma.config.ts at it for the Prisma CLI, while the app reads the 6543 string.
    Environment
    DATABASE_URL="postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@[POOLER-HOST]:6543/postgres?pgbouncer=true"
    DIRECT_URL="postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@[POOLER-HOST]:5432/postgres"
  8. 8
    Create the client once, at module scope, so every request on a warm instance shares it, and keep its pool small. Supabase says to start at 1 connection per instance. On Fluid compute, Vercel says not to cap a pool at 1, to keep a short idle timeout, and to register the pool with attachDatabasePool from @vercel/functions so idle connections close before the instance suspends. Whatever you pick, the pool size times the instances that can be warm at once has to stay under the pooler's client limit for your compute size. The max below is an example, not a recommendation:
    Code
    import { Pool } from 'pg'
    import { attachDatabasePool } from '@vercel/functions'
    
    export const pool = new Pool({
      connectionString: process.env.DATABASE_URL,
      max: 3,
      idleTimeoutMillis: 5000,
    })
    
    attachDatabasePool(pool)
  9. 9
    Check the result after the deploy, under the traffic that caused the error. In the Supabase Dashboard, Observability has a Shared Pooler (Supavisor) Client Connections report, which Supabase notes is not real-time. For a live count of database connections, Supabase's pooling guide gives this query for the SQL Editor:
    SQL
    select
      count(usename),
      count(application_name),
      application_name,
      usename
    from
      pg_stat_ssl
      join pg_stat_activity on pg_stat_ssl.pid = pg_stat_activity.pid
    group by usename, application_name;
  10. 10
    Read a new error as the next step, not as a failed fix. "prepared statement already exists" means a driver still prepares statements. "Tenant or user not found" is the pooler failing to match your host and username to a project, which Supabase's guide lists under troubleshooting: copy the string again from the Connect dialog.
  11. 11
    If "Max client connections reached" continues on 6543, the app has more clients than your compute size allows. Lower each instance's pool (Supabase suggests serverless setups start Prisma at connection_limit=1), close connections the app no longer needs, or move to a larger compute size, which Supabase says also raises the max client limit. Raising Pool Size lets the pooler open more Postgres connections, and Supabase says direct connections plus both poolers' backend connections have to stay below Postgres max connections, with room left for Auth, Storage and PostgREST.

Official docs behind this checklist

FAQ for builders before launch

Should I raise the Pool Size instead?

Not as the first fix. In session mode the Pool Size caps clients for each database role and database combination, so raising it lets more function instances in, but each one still holds a real Postgres connection while it stays connected. Supabase's pooling guide says direct connections plus the backend connections of both poolers have to stay below the maximum for your compute size, and Supabase's own services use some of that. Transaction mode shares connections between clients, which is why Supabase recommends it for serverless functions.

Do I need a pooler string if my app only uses supabase-js?

No. supabase-js calls the Data API over HTTPS, and Supabase says that API has a built-in connection pooler and suits serverless workloads. A connection string matters only for code that talks to Postgres directly, such as Prisma, Drizzle, Postgres.js or node-postgres.

Why did it work locally?

Locally one long-running process holds one client pool. On Vercel every warm function instance creates its own pool, and Supabase's connection guide notes that a few dozen instances with Postgres.js's default of 10 connections is enough to exhaust the pool. Traffic, not your code, decides how many instances run.

Can I use the direct connection from Vercel?

Supabase lists Vercel among the platforms that only accept IPv4 connections, and the direct connection is IPv6 unless the project has the IPv4 add-on, which Supabase offers on Pro plans and above. Even with the add-on, a direct connection has no pooler in front of it, and Supabase's guide keeps it for persistent backends, migrations and pg_dump.