Documentation
Framework guides

Connect with Node.js

Use node-postgres with pooling, TLS, and safe shutdown.

5 minute read ยท Updated September 2026

Install and configure

Terminal
pnpm add pg
pnpm add -D @types/pg

Create one shared pool

db.ts
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.
Was this guide useful?

If anything is unclear or incorrect, tell the support team.

Get help