Imagine a NestJS API that runs fine on three replicas. A busy afternoon triggers autoscaling to six, and the logs fill with FATAL: sorry, too many clients already. Nothing in the code changed. Each new replica simply opened its own pool of database connections, and the database ran out.
This failure is common, it's predictable, and it usually shows up at the worst moment: when traffic is highest.
Fixing it comes down to a few steps:
- Count every connection your system can open, not just the API's.
- Don't solve it by raising
max_connectionsalone. - Put PgBouncer in front of the database, in transaction mode.
- Fix the few things transaction mode breaks.
- Run migrations over a direct connection.
- Watch both the pooler and the database.
Why PostgreSQL runs out of connections
PostgreSQL's documentation describes a process-per-user model: every connection is handled by its own server process. Connections aren't free, so the server caps them. The connection settings say max_connections is typically 100 by default.
Azure Database for PostgreSQL flexible server sets its own limits per compute size. As of November 2026, its limits page says:
- Azure reserves 15 connections for its own use.
- The smallest Burstable size, B1ms, allows 50 connections, of which 35 are for users.
- Changing the compute size doesn't change the connection limit. The
max_connectionsvalue stays the same, and changing the parameter itself requires a restart. - When the limit is reached, clients get exactly the error above.
Do the connection math
Before tuning anything, add up every client that can hold a connection at the same time:
- API replicas × pool size. node-postgres's pool defaults to a maximum of 10 connections. Its
connectionTimeoutMillisdefaults to 0, meaning a request waits for a connection with no timeout. - ORM defaults. Prisma's v6 connection pool defaulted to the number of physical CPUs times two, plus one. Its current documentation takes defaults from the underlying driver instead, so check the version you run. TypeORM exposes a
poolSizeoption in its data source options. - Background workers, such as queue processors, each with their own pool.
- Scheduled jobs and scripts.
- Migrations running during a release.
- Admin tools and someone's open SQL client.
- Deploy surge: during a rolling update, old and new replicas run side by side for a while.
Six replicas with the default node-postgres pool already want 60 connections, nearly double what B1ms allows for users, before any worker or migration connects.
Why not just raise max_connections?
On Azure, Microsoft's limits page advises against raising max_connections beyond the default for your compute size, because each connection costs memory and processing on the server. It recommends a connection pooler instead, specifically PgBouncer in transaction mode, and suggests starting with a pool size of two to five times the number of vCores.
A pooler works because most application connections sit idle most of the time. Many client connections can share a small number of real database connections.
Azure's built-in PgBouncer
Flexible server includes PgBouncer, described in Microsoft's PgBouncer documentation. As of November 2026:
- It runs on port 6432, alongside the normal port 5432.
- It's available on General Purpose and Memory Optimized tiers, not Burstable.
- You turn it on with a server parameter,
pgbouncer.enabled. The parameter reference lists its default as false, while the overview page describes PgBouncer as enabled by default, so check the parameter's value on your own server. Changing it doesn't need a restart. - The defaults: transaction pool mode, a pool size of 50 per user and database pair, and up to 5,000 client connections.
Two details from Microsoft's own documentation are worth knowing:
- Pool size is per user and database pair. If your app connects as two different users, each gets its own pool.
- The built-in PgBouncer has limits. Microsoft notes it can be a single point of failure and that PgBouncer is single-threaded, so very high throughput may need another approach.
Session mode versus transaction mode
PgBouncer's feature documentation describes its pool modes. In session mode, a client keeps the same server connection until it disconnects, which barely helps an API whose pools hold connections open. In transaction mode, a client gets a server connection only for the length of each transaction, then gives it back. That's what lets dozens of replicas share a small number of database connections, and it's why Microsoft recommends it.
The trade-off is that anything which assumes "my connection keeps its state between transactions" stops being safe.
What breaks in transaction mode, in a typical NestJS app
PgBouncer's feature table lists what transaction mode doesn't support, including SET and RESET, LISTEN, cursors declared WITH HOLD, SQL-level PREPARE, some temporary table options, and session-level advisory locks. In a NestJS app, a few of those matter most:
- Setting the tenant for row-level security. If your app runs
SET app.tenant_id = ...once per connection, the next transaction may land on a different server connection with a different tenant. Set it inside each transaction instead. PostgreSQL's SET documentation describesSET LOCAL, which lasts only until the end of the current transaction. - LISTEN and NOTIFY for real-time features. A listener needs a stable session. Give it its own direct connection to port 5432 instead of going through the pooler.
- Advisory locks for jobs. Session-level advisory locks can outlive the transaction on a shared connection. Use transaction-level advisory locks, which are released at commit.
- Prepared statements. These need care, covered next.
Prisma and prepared statements
Most drivers and ORMs use protocol-level prepared statements. PgBouncer added support for them in transaction mode in version 1.21.0, according to its changelog, but only when the max_prepared_statements setting is above zero, per its configuration reference.
Prisma's PgBouncer guide recommends not setting its older pgbouncer=true connection flag when you use PgBouncer 1.21.0 or later, and expects transaction mode with max_prepared_statements above zero.
On Azure, check this setting before you switch Prisma over. The built-in PgBouncer parameter reference lists max_prepared_statements with a default of 0, as of November 2026. If you rely on prepared statements through the pooler, set it to a positive value, or follow Prisma's guidance for your version and test the combination before production.
Run migrations on a direct connection
Migrations shouldn't go through transaction pooling. Prisma's guide says its migration engine doesn't support PgBouncer connection pooling and needs a direct connection to the database. Point your application at port 6432, and your migration step at port 5432, using a separate connection string. Prisma's documentation describes configuring a direct URL for this; the exact setting depends on your Prisma version.
The same applies to any long-running admin session or tool that needs session features.
"Idle in transaction" eats your pool
A transaction that starts and never finishes holds its server connection until it ends. Under transaction pooling, that connection can't be shared, and a few stuck transactions can starve everyone else. PostgreSQL's statistics view shows these as idle in transaction in pg_stat_activity.
PostgreSQL's client connection settings include idle_in_transaction_session_timeout, which is off by default. A modest value closes sessions that sit idle inside a transaction too long. Also make sure your code always commits or rolls back, and releases pooled clients; node-postgres's documentation is explicit that you must release a client back to the pool.
Failover and restarts
On Azure, the built-in PgBouncer runs on the server itself. After a high-availability failover, it restarts on the new primary, and the connection string stays the same, but existing connections drop. Scaling the server or changing some settings also restarts it.
Your app should expect that:
- Set a connection timeout, so requests fail fast instead of waiting forever for a pool that's reconnecting. Remember that node-postgres waits indefinitely by default.
- Retry idempotent operations after a brief delay.
- Make sure startup and health checks cope with the database being briefly unavailable.
On the Burstable tier
If you're on a Burstable size, the built-in PgBouncer isn't available. Your options:
- Keep pools small and explicit, and do the connection math before every scale-out.
- Run your own PgBouncer, as a container next to your app or as a small separate service, with the same transaction-mode caveats.
- Move up a tier when you need both more connections and the built-in pooler.
Monitor both layers
You need to see the database and the pooler. On Azure, the built-in PgBouncer metrics are opt-in through the metrics.pgbouncer_diagnostics parameter, per Microsoft's monitoring documentation. Once enabled, watch:
- Clients waiting for a connection, which shows the pool is too small or connections are being held too long.
- Active server connections, compared with the pool size.
- Database active connections, compared with
max_connections. - Long transactions and sessions by state, to catch
idle in transactionproblems.
Alert on waiting clients and on database connections approaching the limit, so you hear about it before users do.
Checklist
- Count every connection source: replicas, workers, cron, migrations, admin, and deploy surge.
- Know your limit: Azure reserves 15, and B1ms leaves 35 for users.
- Don't rely on raising max_connections. Use a pooler.
- Enable the built-in PgBouncer on General Purpose or Memory Optimized tiers, and connect the app to port 6432.
- Use transaction mode, and size pools from Microsoft's starting point.
- Move per-connection settings into transactions with
SET LOCAL. - Give LISTEN/NOTIFY its own direct connection.
- Use transaction-level advisory locks.
- Check the prepared-statement setting (
max_prepared_statements) before pointing Prisma or other drivers at the pooler. - Run migrations on a direct connection to port 5432.
- Set idle-in-transaction and connection timeouts, and always release clients.
- Expect connections to drop on failover, and retry safely.
- Turn on pooler metrics and alert on waiting clients.



