Two different limits
Postgres has a fixed number of connection slots, set by your compute size. When they are used up you get remaining connection slots are reserved … (Postgres 16 and later word it … reserved for roles with the SUPERUSER attribute) or sorry, too many clients already.
Supavisor, Supabase's connection pooler, also has a client limit per compute size. Exceeding it gives Max client connections reached. Both usually have the same root cause: many app instances, each opening its own pool.
Note: supabase-js does not open database connections. It talks HTTP to the Data API, which has its own small pool. This problem comes from ORMs and drivers (Prisma, Drizzle, node-postgres, postgres.js, SQLAlchemy) connecting directly.
Step 1: see who holds the connections
select usename, application_name, client_addr, state, count(*)
from pg_stat_activity
group by 1, 2, 3, 4
order by 5 desc;
show max_connections;Lots of idle connections from one app means pools that are too big or never closed. Supavisor errors show up in the dashboard's Pooler logs.
Step 2: use the right connection string
From Supabase's connection guide:
- Transaction mode, port 6543: for serverless and edge runtimes (Vercel, Netlify, AWS Lambda, Cloudflare Workers) that open many short-lived connections.
- Session mode, port 5432 on the pooler host: for long-running servers on an IPv4-only network.
- Direct connection (
db.<ref>.supabase.co:5432): long-running servers and migrations. IPv6 unless you have the IPv4 add-on.
postgresql://postgres.<project-ref>:<password>@<pooler-host>:6543/postgresCopy the pooler host from the Connect dialog; it can't be guessed from the region.
Step 3: turn off prepared statements in transaction mode
The shared pooler doesn't support prepared statements in transaction mode. Turn them off in your driver:
// postgres.js / Drizzle
const sql = postgres(process.env.DATABASE_URL, { prepare: false });
# Prisma: pooled URL for the app, direct URL for migrations
DATABASE_URL="postgresql://postgres.<ref>:<password>@<pooler-host>:6543/postgres?pgbouncer=true&connection_limit=1"
DIRECT_URL="postgresql://postgres.<ref>:<password>@<pooler-host>:5432/postgres"Step 4: keep each pool small
- In serverless functions, a pool of 1 to a few connections per instance. Fifty instances times a default pool of ten is five hundred connections.
- Create the client once per process, outside the request handler. In Next.js development, keep it on
globalThisso hot reload doesn't create a new pool on every save. - Set an idle timeout so unused connections are closed.
// node-postgres, created once at module level
import pg from 'pg';
export const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
max: 3,
idleTimeoutMillis: 10_000,
});Emergency: free slots now
select pg_terminate_backend(pid)
from pg_stat_activity
where state = 'idle'
and state_change < now() - interval '10 minutes'
and pid <> pg_backend_pid();This only buys time. If the connection count climbs back, the pools are still too large. Upgrade compute only after pooling is right; otherwise the bigger limit fills up the same way.
How to verify
Rerun the pg_stat_activity query under normal load and during a traffic spike. The total should stay well below max_connections, and the app's connections should come through the pooler, not as hundreds of direct idle sessions.
Related guides
- “relation does not exist” in Postgres and Supabase – schema, quotes, search_path, cache
- Supabase Realtime subscribed but no postgres_changes events – the checklist
- Postgres “new row for relation … violates check constraint” – find the rule and fix it
- Supabase rpc “Could not find the function … in the schema cache” and “function does not exist”
More in Postgres errors and performance · all guides