Skip to content

pg

This page explains how to use ts-sql-query with the pg driver. It covers two approaches: using a connection pool or using a single connection directly.

Supported databases

Tested with

pg ^8.23.1 on Node.js and Bun.

This information reflects the driver version pinned in this project's devDependencies and the runtimes the test suite runs it on against a real database. Other compatible versions may work but are not actively tested.

Do not share connections between requests

A ts-sql-query connection object — along with the query runner instances passed to its constructor — represents a dedicated connection to the database.

Therefore, you must not share the same connection object between concurrent HTTP requests. Instead, create a new connection object for each request, along with its own query runners.

Even if the query runner internally uses a connection pool, the ts-sql-query connection still represents a single active connection, acquired from the pool. It must be treated as such and never reused across requests.

Using a connection pool

Executes queries through a pg connection obtained from a pool.

import { Pool } from 'pg';
import { PgPoolQueryRunner } from "ts-sql-query/queryRunners/PgPoolQueryRunner";

const pool = new Pool({
    user: 'dbuser',
    host: 'database.server.com',
    database: 'mydb',
    password: 'secretpassword',
    port: 3211,
});

async function main() {
    const connection = new DBConnection(new PgPoolQueryRunner(pool));
    // Do your queries here
    connection // ...
}

Warning

If you want to allow to have nested transactions you must create the instance as new PgPoolQueryRunner(pool, {allowNestedTransactions: true})

Using a single connection

Executes queries through a dedicated pg connection.

import { Pool } from 'pg';
import { PgQueryRunner } from "ts-sql-query/queryRunners/PgQueryRunner";

const pool = new Pool({
    user: 'dbuser',
    host: 'database.server.com',
    database: 'mydb',
    password: 'secretpassword',
    port: 3211,
});

async function main() {
    const pgConnection = await pool.connect();
    try {
        const connection = new DBConnection(new PgQueryRunner(pgConnection));
        // Do your queries here
        connection // ...
    } finally {
        pgConnection.release();
    }
}

Warning

If you want to allow to have nested transactions you must create the instance as new PgQueryRunner(pgConnection, {allowNestedTransactions: true})

Inside a transaction something else manages

When the connection is already in a transaction that something else began and ends, like your own code or another library, create the query runner with forUseInTransaction. Its statements run in that transaction, and a transaction can't begin on it: connection.transaction(...) and connection.beginTransaction() reject with NESTED_TRANSACTION_NOT_SUPPORTED. Without the option, the commit of that inner transaction would end the outer one.

For ts-sql-query there is no transaction open, so executeAfterNextCommit, the other end-of-transaction functions and getTransactionMetadata() throw NOT_IN_TRANSACTION.

import type { PoolClient } from 'pg';
import { PgQueryRunner } from "ts-sql-query/queryRunners/PgQueryRunner";

async function main(pgConnection: PoolClient) {
    const connection = new DBConnection(new PgQueryRunner(pgConnection, { forUseInTransaction: true }));
    // Do your queries here, in the transaction
    connection // ...
}

Running a statement on each new connection

Some settings are properties of the database session, not of the query — notably the session time zone. To pin it without touching your schema, run the statement once per connection, when the pool opens it. The pg pool emits a connect event on each brand-new client for exactly this:

import { Pool } from 'pg';

const pool = new Pool({ /* … */ });

pool.on('connect', (client) => {
    // Runs once per newly created pooled connection.
    client.query("SET TIME ZONE 'UTC'"); // session time zone — see the Time zones page
});

This is what the Time zones page recommends aligning on connect. PostgreSQL has no session collation (unlike Oracle), so a case-insensitive comparison is set on the column / database collation or per value with .collate() instead — see the Collations page.