Connect with Node.js
Use node-postgres with pooling, TLS, and safe shutdown.
5 min readInstall and configure
pnpm add pg
pnpm add -D @types/pgCreate one shared pool
import { Pool } from "pg";
export const db = new Pool({
connectionString: process.env.DATABASE_URL,
max: 10,
connectionTimeoutMillis: 5_000,
idleTimeoutMillis: 30_000,
ssl: { rejectUnauthorized: true },
});Application behavior
- Create one pool per application process, not per request.
- Use parameterized queries.
- Release checked-out clients in finally blocks.
- Close the pool during graceful shutdown.
- Keep the combined pool size of all replicas below the role connection limit.
Something unclear or wrong? Tell support