Overview
@effect/sql-pg provides a PostgreSQL driver for Effect’s SQL toolkit. It uses the pg library under the hood and provides full support for connection pooling, transactions, prepared statements, and PostgreSQL-specific features.
Installation
Basic Usage
Creating a Client
Use thePgClient.layer function to create a PostgreSQL client layer:
Connection with URL
You can also connect using a connection URL:Executing Queries
Configuration Options
Connection Options
| Option | Type | Description |
|---|---|---|
url | Redacted | PostgreSQL connection URL |
host | string | Database host |
port | number | Database port (default: 5432) |
database | string | Database name |
username | string | Username for authentication |
password | Redacted | Password for authentication |
ssl | boolean | ConnectionOptions | Enable SSL/TLS connection |
path | string | Unix socket path |
Connection Pool Options
| Option | Type | Description |
|---|---|---|
maxConnections | number | Maximum number of connections in the pool |
minConnections | number | Minimum number of connections to maintain |
connectionTTL | Duration.Input | Time-to-live for connections |
idleTimeout | Duration.Input | How long a connection can be idle |
connectTimeout | Duration.Input | Timeout for establishing connections (default: 5s) |
Transform Options
| Option | Type | Description |
|---|---|---|
transformResultNames | (str: string) => string | Transform column names in results |
transformQueryNames | (str: string) => string | Transform identifiers in queries |
transformJson | boolean | Apply transformations to JSON fields |
Advanced Options
| Option | Type | Description |
|---|---|---|
applicationName | string | Application name for PostgreSQL (default: “@effect/sql-pg”) |
spanAttributes | Record<string, unknown> | Custom telemetry attributes |
types | Pg.CustomTypesConfig | Custom PostgreSQL type configuration |
PostgreSQL-Specific Features
JSON Support
Handle JSON/JSONB columns with proper encoding:LISTEN/NOTIFY
Subscribe to PostgreSQL notifications:Transactions
Automatic transaction management:Name Transformations
Automatically convert between naming conventions:Error Handling
All SQL errors are wrapped inSqlError:
