The app worked fine in testing. Then traffic picked up, or you deployed an API route to Vercel or Netlify, and requests started failing with Max client connections reached. Your driver may print it as FATAL: Max client connections reached. Restarting things helps for a few minutes, then it comes back.
This guide is for apps with server code that talks to a Supabase database over a Postgres connection string, including backends added to projects built in Lovable, Bolt or v0. It starts with the one question that decides the fix: which connection string is your code using? Supabase help: /fix/supabase.
What the error is telling you
The message comes from Supabase’s connection pooler, not from your queries. Supabase lists it among the shared pooler (Supavisor) errors that show up in the dashboard’s Pooler Logs, and ties it to the client limit of your compute size (Supabase: Supavisor).
A pooler tracks two separate numbers (Supabase: connection pooling and limits):
- Client connections: the number of clients that may be attached to the pooler at once. Your compute size fixes this number.
- Backend connections: the number of real Postgres connections the pooler opens on their behalf. The Pool Size setting controls this one.
Which limit you hit depends on the port in your connection string. Supabase’s write-up on this error explains both cases (Supabase discussion #22305; Prisma troubleshooting):
- Port 6543, transaction mode: you tried to attach more clients than your compute size allows. On a compute size whose pooler takes 200 clients, client number 201 is refused.
- Port 5432 on the pooler host, session mode: each client holds one backend connection until it disconnects, so the real ceiling is Pool Size for each role and database pair. With Pool Size 15, the 16th client waits in a queue for up to a minute, then gets this error if nobody disconnects.
Supabase publishes these defaults per compute size (Supabase: compute and disk):
| Compute | Pooler client limit | Pool Size default | Postgres max_connections |
|---|---|---|---|
| Nano (Free) | 200 | 15 | 60 |
| Micro | 200 | 15 | 60 |
| Small | 400 | 15 | 90 |
| Medium | 600 | 15 | 120 |
| Large | 800 | 20 | 160 |
The pooler client limit cannot be changed by itself; the dashboard’s Max client connections value in Database Settings follows your compute size (Supabase: updating pool settings). So the useful question is why your app is opening more clients than that limit.
Step 1: find which connection string your code uses
Open wherever your server code gets its database URL: the environment variables on Vercel or Netlify (names such as DATABASE_URL, POSTGRES_URL or DIRECT_URL), your .env file, or the Edge Function secrets. Then match the host, port and username against this table, built from Supabase’s connection docs (Supabase: connect to your database):
| What you see in the string | Connection type | Good for |
|---|---|---|
db.<ref>.supabase.co:5432, user postgres | Direct connection | Long-running servers on IPv6 (or with the IPv4 add-on), migrations, pg_dump |
<cluster>.pooler.supabase.com:5432, user postgres.<ref> | Shared pooler, session mode | Long-running servers on IPv4, database GUIs |
<cluster>.pooler.supabase.com:6543, user postgres.<ref> | Shared pooler, transaction mode | Serverless functions and Edge Functions |
db.<ref>.supabase.co:6543, user postgres | Dedicated pooler (Pro Plan and up) | High-load apps; transaction mode only; IPv6 or the IPv4 add-on |
Two things to know before you change anything:
- The pooler host is not something you can build yourself. It contains a cluster index as well as the region, and one region may host several clusters. Copy it from the Connect button at the top of your project dashboard.
- The username changes with the pooler. The shared pooler expects
postgres.<project-ref>, while plainpostgresis the user for direct connections and for the dedicated pooler.
If you can’t find a connection string at all
Then your own code may not be the source. A frontend that uses supabase-js with the project URL and the public key goes through the Data API over HTTPS, not a Postgres connection (Supabase: connect to your database). That API has a built-in connection pooler (Supabase: serverless drivers). In that case look at what else connects through Supavisor: a database GUI or BI tool left open on the session pooler, a cron worker on another host, or a second app sharing the project. Supabase Storage also uses Supavisor internally, as its own user, database and mode combination, so the Pool Size you set applies to it too (Supabase: Supavisor FAQ).
Fix 1: send serverless traffic to the transaction pooler
Serverless platforms start instances as traffic grows, and each warm instance holds its own connections. Supabase’s guidance is to point serverless and Edge Function code at transaction mode on the shared pooler, which listens on 6543 (Supabase: connect to your database). In transaction mode a client gets a backend connection only while a transaction runs, then gives it back, so a small pool can serve a large number of clients.
- Open your project, press Connect at the top, and pick Transaction pooler.
- Copy the string and put in your database password. Percent-encode characters such as
&,#,?or a space. - Replace the value in your host’s environment variables. On Vercel, redeploy so the new value is picked up.
If your string was on port 5432 of the pooler host, this switch alone removes the session-mode ceiling described above. Session mode remains available for clients that need a long-held connection, such as a GUI.
If you use the @vercel/postgres package, Supabase’s serverless page appends ?workaround=supabase-pooler.vercel at the end of the transaction pooler string and stores it as POSTGRES_URL (Supabase: serverless drivers).
Fix 2: turn off prepared statements
Right after the switch to port 6543 you may see a new error: prepared statement "s0" already exists, or a similar name. The shared pooler can’t run prepared statements while it is in transaction mode, and several drivers create them behind the scenes (Supabase: prepared statement already exists). Turn them off in the driver (Supabase: disabling prepared statements):
| Driver | Setting |
|---|---|
| Prisma | append ?pgbouncer=true to the 6543 string |
| Postgres.js, Drizzle with Postgres.js | prepare: false |
node-postgres (pg) | don’t pass a name in query objects |
| Psycopg | prepare_threshold=None |
| JDBC | prepareThreshold=0 |
| asyncpg | statement_cache_size=0 |
Prisma’s own docs describe pgbouncer=true as the switch that stops it from relying on named prepared statements behind a pooler. The same page recommends leaving it off for PgBouncer 1.21.0 or later, which matters if you move to Supabase’s dedicated pooler (Prisma: PgBouncer).
One more caveat for Postgres.js: it pipelines queries by default, and transaction mode does not support pipelining. Supabase warns that mixing the two may stall queries or hand back rows that belong to another query, and that turning pipelining off with max_pipeline: 0 stops sql.begin() transactions from working (Supabase: Postgres.js; Supabase: connect to your database). If you see strange results after the switch, that is a reason to try a different driver or a session-mode connection from a long-running server.
Fix 3: create the client once and keep each pool tiny
The pooler counts clients, and your driver keeps its own pool of them. Postgres.js opens up to 10 connections per instance by default, and Supabase points out that a few dozen warm instances are enough to use up the pooler (Supabase: connect to your database). Supabase’s advice for serverless code: build the client at module scope, not inside the request handler, and give it a pool of one.
Postgres.js or Drizzle:
// db.ts — imported by your route handlers, never re-created per request
import postgres from 'postgres'
import { drizzle } from 'drizzle-orm/postgres-js'
const client = postgres(process.env.DATABASE_URL!, {
max: 1, // one connection per warm instance
prepare: false, // required on port 6543
ssl: 'require',
})
export const db = drizzle(client)
Prisma 6 and earlier read the pool size from the URL. The prisma user here is the custom user from Supabase’s Prisma guide (Supabase: Prisma); if you did not create one, keep the postgres.<ref> user from your Connect dialog:
DATABASE_URL="postgres://prisma.<ref>:<password>@<pooler-host>:6543/postgres?pgbouncer=true&connection_limit=1"
Prisma 7 uses driver adapters, and the pool size moves into the adapter options; the connection_limit URL parameter no longer sets it. The pg adapter defaults to 10 (Prisma: connection pool):
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from './generated/prisma/client'
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL, // the 6543 string with ?pgbouncer=true
max: 1,
})
export const prisma = new PrismaClient({ adapter })
Supabase’s Prisma notes say to start serverless setups at one connection and raise it only with care (Prisma troubleshooting). Raise it only when you can see requests on one instance queuing for the connection.
Check your code for these patterns, which multiply clients:
postgres(...),new Pool(...)ornew PrismaClient()inside a request handler, API route or React Server Component.- A new client per hot reload in local development. Keep a single instance on
globalThisin dev if your framework re-imports modules. - Background jobs or webhooks that open a client and never close it.
For Supabase Edge Functions, the docs’ Deno Postgres example creates the pool once with a size of one and releases each connection in a finally block (Supabase: Edge Functions and Postgres). If the function only reads and writes tables, the supabase-js client avoids Postgres connections altogether. When an Edge Function returns errors instead of data, Supabase Edge Function non-2xx status code covers the other causes.
Fix 4: keep migrations and admin tools off port 6543
Migrations, pg_dump, restores and database GUIs are long single sessions, so Supabase sends them to a direct connection, or to session mode on IPv4-only networks (Supabase: connect to your database). With Prisma that means two variables: DATABASE_URL on 6543 for the app, and DIRECT_URL on 5432 for Prisma CLI commands, set as the data source URL in prisma.config.ts (Supabase: Prisma).
Keep those tools from staying connected around the clock. A GUI parked on the session pooler holds a backend connection for as long as it stays connected.
Find what is holding connections
Paste this into the SQL Editor to list who holds Postgres connections and in what state:
select usename as role,
application_name,
state,
count(*) as connections,
max(now() - state_change) as longest_in_state
from pg_stat_activity
where backend_type = 'client backend'
group by 1, 2, 3
order by connections desc;
The role tells you the source. authenticator is the Data API, supabase_auth_admin is Auth, supabase_storage_admin is Storage, and postgres covers the Supabase Dashboard and external tools, including your own code unless it uses a custom role such as prisma (Supabase: connection management). A role you created just for the app makes this split much easier to read.
Look hard at idle in transaction. Such a session opened a transaction and never committed or rolled back, so it keeps its locks and its connection while doing nothing. A query that was cancelled will not close it; ending the session will (Supabase: connection management):
select pid, usename, application_name, now() - xact_start as open_for, query
from pg_stat_activity
where state = 'idle in transaction'
order by xact_start;
-- after checking the row, end that one session:
select pg_terminate_backend(<pid>);
Warning: pg_terminate_backend ends that session’s connection entirely (Supabase: connection management). Postgres rolls back its open transaction, so any uncommitted changes in it are lost, and the app that owned it gets a connection error on its next query. Run it only on a pid you have checked in the query above, never on every row. The dashboard’s Database Connections page shows the same session states and can terminate a stuck session without SQL.
Then find the code path that left it open, such as a transaction with an early return or a thrown error that skips the rollback.
For the pooler side, the dashboard’s Observability section has a Shared Pooler (Supavisor) Client Connections report. It refreshes on a delay, so use pg_stat_activity for live numbers (Supabase: connection pooling and limits).
Functions that hang after a quiet period
A different symptom with the same setup: CONNECT_TIMEOUT, or queries that stall until the function times out. Supabase traces it to serverless runtimes freezing an instance between requests, which leaves a stale socket. Its suggestions include the Data API for read-heavy paths, a quick select 1 check with a short timeout before the real query, Vercel Fluid Compute with attachDatabasePool, and retrying connection timeouts (Supabase: CONNECT_TIMEOUT in serverless functions).
If it keeps happening
Work through these in order, checking the logs after each one.
- Lower the per-instance pool further. If any pool is still above one, bring it down and confirm only one client is created per instance.
- Shorten queries. A slow query holds its backend connection longer, which makes clients queue. Supabase lists simpler queries and indexes among its fixes for this error (Prisma troubleshooting).
- Review Pool Size. In Database Settings, Pool Size caps the Postgres connections each pooler may open, applied per role (Supabase: connection management). Supabase suggests staying near 40% of the database’s max connections if you lean on the Data API, and up to about 80% if you don’t, so Auth and other services keep room. Base the number on your measured peak, not the maximum.
- Consider the dedicated pooler if you’re on the Pro Plan or higher. It runs next to the database, works in transaction mode only, and handles prepared statements from PgBouncer version 1.21 onward (Supabase: connect to your database). It has its own client limit, separate from the shared pooler’s (Supabase: connection pooling and limits).
- Increase compute size last. It is the only way to raise the pooler’s client limit, and it also makes queries finish faster (Supabase discussion #22305). Do it after the fixes above, or the extra headroom fills the same way.
Raising Postgres max_connections by hand is a separate setting that does not change the pooler limit. Supabase warns that it then stays fixed through compute changes and costs memory for every connection (Supabase: changing max database connections).
A prompt for your AI builder
If Lovable, Bolt, v0 or another assistant wrote the server code, give it a precise task instead of “fix the database error”:
Our server code gets "Max client connections reached" from Supabase.
1. List every file that creates a database client (postgres(), new Pool(),
new PrismaClient(), drizzle()) and say whether it runs per request.
2. Move client creation to one module-level instance and import it everywhere.
3. Set the pool size to 1 and turn off prepared statements
(prepare: false for postgres.js; pgbouncer=true in the URL for Prisma).
4. Do not change the connection string values or any Supabase settings;
I will update DATABASE_URL to the transaction pooler (port 6543) myself.
When done, list every file you changed.
Paste connection strings into your host’s environment settings yourself, never into the chat. If the app also fails at build time on Vercel, see v0 deploy error. For a wider pre-launch pass, see the AI app production readiness checklist.
FAQ
What is the difference between ports 5432 and 6543?
On the pooler host, 5432 is session mode, where each client keeps one Postgres connection until it disconnects, and 6543 is transaction mode, where clients share connections between transactions. On the db.<ref>.supabase.co host, 5432 is a direct connection and 6543 is the dedicated pooler (Supabase: connect to your database).
Why do I get “prepared statement already exists” after switching?
Your driver still creates prepared statements, and the shared pooler’s transaction mode does not support them. Add pgbouncer=true for Prisma or prepare: false for Postgres.js and Drizzle, or move that client to port 5432 (Supabase: prepared statement already exists).
Does my AI-built app need a database connection string at all?
Not for browser code. The supabase-js client uses your project URL and public key over the Data API and needs Row Level Security on your tables. Only server code using Prisma, Drizzle, Postgres.js or another Postgres driver needs a connection string (Supabase: connect to your database). If your project has no such code, look for an outside tool or service connecting to the database.
How do I check the number of open connections?
For a live count, query pg_stat_activity in the SQL Editor as shown above. For trends, use the Database Connections and Shared Pooler Client Connections reports under Observability in the dashboard (Supabase: connection pooling and limits).
Comments