Skip to content

PostgreSQL Driver

Setup

ts
import { Db, createPostgresDriver } from "typhex";

const db = new Db(
  createPostgresDriver({
    connectionString: process.env.TYPHEX_POSTGRES_URL!,
  }),
);

Options

OptionTypeDescription
connectionStringstringStandard PostgreSQL URI: postgresql://user:password@host:port/database
urlstringAlias for connectionString, useful in config files
hoststringHostname when not using a connection string
portnumberPort, default 5432
databasestringDatabase name, default "postgres"
userstringDatabase user
passwordstringDatabase password
sslpg valueSSL config forwarded to pg
poolMinnumberMinimum pool connections, default 2
poolMaxnumberMaximum pool connections, default 10
idleTimeoutMsnumberIdle connection timeout in milliseconds, default 30000
connectionTimeoutMsnumberPool connection timeout in milliseconds, default 5000
statementTimeoutMsnumberPostgreSQL statement_timeout in milliseconds
loggerobjectCustom pool error logger with an error(message, err) method

Installation

bash
npm install pg

The driver uses pg.Pool internally; call db.close() on shutdown to release pool connections.

Column Types

Use PostgreSQL-native types in your schema:

ts
const User = Entity("users", {
  id: "SERIAL PRIMARY KEY",
  name: "VARCHAR(255) NOT NULL",
  age: "INTEGER NOT NULL",
});

The query API (where, insert, select, etc.) is identical to SQLite — only the driver and column types differ.

Released under the MIT License.