Under load, the app starts throwing connection errors because PostgreSQL's max_connections limit has been reached, usually from unclosed connections or missing pooling in a serverless environment.
Add a connection pooler like PgBouncer in front of Postgres, and make sure the app closes or properly reuses connections instead of opening a new one per request.
Step-by-Step Guide
Check current connection count: SELECT count(*) FROM pg_stat_activity;
Review max_connections in postgresql.conf before raising it blindly
Use a pooling library appropriate to your stack (pg-pool, Prisma's connection_limit)
Set an idle_timeout so unused connections close automatically
Add PgBouncer for transaction-level pooling in front of Postgres
In serverless functions, ensure the ORM client closes connections after each invocation
Monitor pg_stat_activity for idle-in-transaction sessions that never commit
Restart app instances to clear any already-leaked connections
Found an issue with this solution?