PostgreSQL
PostgreSQL is a relational DBMS focused on correctness and extensibility.
Setup
Install
npm install @primate/postgresqlYou need a running PostgreSQL server and a database/user with access.
Configure
// config/db.ts
import postgresql from "@primate/postgresql";
export default postgresql({
database: "app",
// host: "localhost",
// port: 5432,
// username: "postgres",
// password: "secret",
// pool: { max: 10, connectTimeout: 30_000, idleTimeout: 0 },
});
Options
| option | type | default | description |
|---|---|---|---|
| database | string |
— | Database name |
| host | string |
"localhost" |
PostgreSQL host |
| port | number |
5432 |
PostgreSQL port |
| username | string |
— | Username |
| password | string |
— | Password |
| pool | object |
driver defaults | Connection pool controls |
Pool options
| option | type | postgres.js default | description |
|---|---|---|---|
| max | number |
10 |
Maximum connections; integer from 1 to 65,535 |
| connectTimeout | number |
30000 |
Milliseconds allowed to establish and authenticate a new connection; integer from 1 to 2,147,483,647 |
| idleTimeout | number |
0 |
Milliseconds before an idle connection closes; integer from 0 to 2,147,483,647, with 0 disabling idle closure |
These controls do not impose a timeout on requests waiting for pool capacity. Omitting an option preserves the postgres.js default.
Independent pools
Create a separately owned PostgreSQL instance without repeating credentials:
const locks = db.fork({
pool: { max: 4, connectTimeout: 5_000, idleTimeout: 1_000 },
});
try {
await locks.client.begin(async sql => {
await sql`SELECT pg_advisory_xact_lock(10, 20)`;
// transaction-scoped work
});
} finally {
await locks.close();
}The fork inherits the parent's endpoint, credentials, and unspecified pool
settings, but owns another pool. Its client, typed sql() queries, Store
operations, transactions, and close() are independent from the parent.
If a transaction connection is lost, client.begin() rejects, including after
an advisory lock was acquired. Rejection reports the loss but cannot cancel
application work already running in the callback. Use application-level
cancellation or fencing when work must stop after lock loss.