Skip to main content

Overview

PostgreSQL dialect that uses the pg library (node-postgres). This is the recommended way to connect Kysely to PostgreSQL databases. Source: src/dialect/postgres/postgres-dialect.ts:43

Installation

Install the required peer dependencies:

Basic Usage

Configuration

PostgresDialectConfig

Source: src/dialect/postgres/postgres-dialect-config.ts:6
pool
PostgresPool | (() => Promise<PostgresPool>)
required
A postgres Pool instance or a function that returns one.If a function is provided, it’s called once when the first query is executed. This allows for lazy initialization of the connection pool.See node-postgres Pool documentation for available pool options.
cursor
PostgresCursorConstructor
Optional cursor constructor for streaming large result sets.Requires the pg-cursor package.
onCreateConnection
(connection: DatabaseConnection) => Promise<void>
Called once for each created connection. Useful for setting up connection-level configuration.
onReserveConnection
(connection: DatabaseConnection) => Promise<void>
Called every time a connection is acquired from the pool. Use this for per-query setup that needs to run every time.

Connection Pooling

The PostgreSQL dialect uses the pg library’s built-in connection pooling. Here are some recommended pool settings:

Streaming with Cursors

For large result sets, you can use cursors to stream results:
1

Install pg-cursor

2

Configure the dialect

3

Use streaming queries

SSL/TLS Configuration

To connect using SSL:

Environment Variables

The pg library automatically reads these environment variables:
  • PGHOST - Database host
  • PGPORT - Database port
  • PGDATABASE - Database name
  • PGUSER - Database user
  • PGPASSWORD - Database password

PostgresPool

Source: src/dialect/postgres/postgres-dialect-config.ts:52 The subset of the pg Pool interface that Kysely requires:

PostgresPoolClient

Source: src/dialect/postgres/postgres-dialect-config.ts:57