Skip to main content

PgDatabase

The main database client class for PostgreSQL. Created by calling driver-specific functions like drizzle() from drizzle-orm/node-postgres, drizzle-orm/postgres-js, etc.

Query Methods

select()

Creates a SELECT query. Call with no arguments to select all columns, or pass a selection object to specify columns.
SelectedFields
Selection object mapping field names to column references or SQL expressions
PgSelectBuilder
Query builder for chaining additional methods like .from(), .where(), .orderBy(), etc.

selectDistinct()

Adds DISTINCT to the SELECT query, returning only unique values.
SelectedFields
Selection object for which columns to select

selectDistinctOn()

PostgreSQL-specific method that adds DISTINCT ON to specify how unique rows are determined.
(PgColumn | SQLWrapper)[]
required
Array of columns or SQL expressions that define uniqueness
SelectedFields
Selection object for which columns to select

insert()

Creates an INSERT query. Use .values() to specify rows to insert.
PgTable
required
The table to insert into
PgInsertBuilder
Query builder with methods like .values(), .onConflictDoNothing(), .returning(), etc.

update()

Creates an UPDATE query. Use .set() to specify values and .where() to filter rows.
PgTable
required
The table to update
PgUpdateBuilder
Query builder with methods like .set(), .where(), .returning(), etc.

delete()

Creates a DELETE query. Use .where() to specify which rows to delete.
PgTable
required
The table to delete from
PgDeleteBase
Query builder with methods like .where(), .returning(), etc.

Common Table Expressions (CTEs)

$with()

Defines a CTE (Common Table Expression) for use in subsequent queries.
string
required
Alias name for the CTE
WithBuilder
Object with .as() method to define the CTE query

with()

Incorporates previously defined CTEs into the main query.
WithSubquery[]
required
One or more CTEs to incorporate
object
Object with query methods: select, selectDistinct, selectDistinctOn, update, insert, delete

Advanced Features

execute()

Executes raw SQL queries or SQLWrapper objects.
SQLWrapper | string
required
SQL query to execute

transaction()

Executes queries within a database transaction.
(tx: PgTransaction) => Promise<T>
required
Async function that receives transaction client
PgTransactionConfig
Transaction configuration options:
  • isolationLevel: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable'
  • accessMode: 'read only' | 'read write'
  • deferrable: boolean

refreshMaterializedView()

Refreshes a PostgreSQL materialized view.
PgMaterializedView
required
The materialized view to refresh

$count()

Counts rows in a table or view with optional filters.
PgTable | PgViewBase | SQL | SQLWrapper
required
Table, view, or SQL to count rows from
SQL
Filter condition for counting

Relational Queries

When using a schema with relations, the query object provides a type-safe relational query API.

Read Replicas

withReplicas()

Configures read replica support for read-write splitting.
PgDatabase
required
Primary database connection for write operations
[PgDatabase, ...PgDatabase[]]
required
Array of replica connections for read operations
(replicas: PgDatabase[]) => PgDatabase
Custom function to select which replica to use. Defaults to random selection.