|
| 1 | +--- |
| 2 | +title: PostgreSQL Driver |
| 3 | +description: PostgreSQL driver configuration schema |
| 4 | +--- |
| 5 | + |
| 6 | +# PostgreSQL Driver |
| 7 | + |
| 8 | +<Callout type="info"> |
| 9 | +**Source:** `packages/spec/src/data/driver/postgres.zod.ts` |
| 10 | +</Callout> |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +PostgreSQL driver configuration schema defines connection settings and options specific to PostgreSQL databases. |
| 15 | + |
| 16 | +## TypeScript Usage |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { PostgresConfigSchema } from '@objectstack/spec/data/driver/postgres'; |
| 20 | +import type { PostgresConfig } from '@objectstack/spec/data/driver/postgres'; |
| 21 | + |
| 22 | +// Validate PostgreSQL configuration |
| 23 | +const config: PostgresConfig = PostgresConfigSchema.parse({ |
| 24 | + database: 'myapp', |
| 25 | + host: 'localhost', |
| 26 | + port: 5432, |
| 27 | + username: 'postgres', |
| 28 | + password: 'secret', |
| 29 | + ssl: false, |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## PostgresConfig |
| 36 | + |
| 37 | +PostgreSQL connection configuration schema. |
| 38 | + |
| 39 | +### Properties |
| 40 | + |
| 41 | +| Property | Type | Required | Description | |
| 42 | +| :--- | :--- | :--- | :--- | |
| 43 | +| **url** | `string` | optional | Connection URI. Format: postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...] | |
| 44 | +| **database** | `string` | ✅ | Database name | |
| 45 | +| **host** | `string` | optional | Hostname or IP address (default: 'localhost') | |
| 46 | +| **port** | `number` | optional | Port number (default: 5432) | |
| 47 | +| **username** | `string` | optional | Authentication username | |
| 48 | +| **password** | `string` | optional | Authentication password | |
| 49 | +| **schema** | `string` | optional | Default schema for tables (default: 'public') | |
| 50 | +| **ssl** | `boolean \| object` | optional | Enable SSL/TLS connection | |
| 51 | +| **applicationName** | `string` | optional | Sets the application_name configuration parameter | |
| 52 | +| **max** | `number` | optional | Maximum number of clients in pool (default: 10) | |
| 53 | +| **min** | `number` | optional | Minimum number of clients in pool (default: 0) | |
| 54 | +| **idleTimeoutMillis** | `number` | optional | Idle timeout in milliseconds | |
| 55 | +| **connectionTimeoutMillis** | `number` | optional | Connection timeout in milliseconds | |
| 56 | +| **statementTimeout** | `number` | optional | Statement timeout in milliseconds | |
| 57 | + |
| 58 | +### SSL Configuration |
| 59 | + |
| 60 | +When `ssl` is an object, it can contain: |
| 61 | + |
| 62 | +| Property | Type | Description | |
| 63 | +| :--- | :--- | :--- | |
| 64 | +| **rejectUnauthorized** | `boolean` | Whether to reject unauthorized certificates | |
| 65 | +| **ca** | `string` | CA certificate | |
| 66 | +| **key** | `string` | Client key | |
| 67 | +| **cert** | `string` | Client certificate | |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Examples |
| 72 | + |
| 73 | +### Basic Configuration |
| 74 | + |
| 75 | +```typescript |
| 76 | +const config: PostgresConfig = { |
| 77 | + database: 'crm', |
| 78 | + host: 'localhost', |
| 79 | + port: 5432, |
| 80 | + username: 'app_user', |
| 81 | + password: 'secure_password', |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +### Connection URI |
| 86 | + |
| 87 | +```typescript |
| 88 | +const config: PostgresConfig = { |
| 89 | + url: 'postgresql://user:password@localhost:5432/mydb', |
| 90 | + database: 'mydb', // Required even with URL |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +### SSL Configuration |
| 95 | + |
| 96 | +```typescript |
| 97 | +const config: PostgresConfig = { |
| 98 | + database: 'production', |
| 99 | + host: 'db.example.com', |
| 100 | + port: 5432, |
| 101 | + username: 'app_user', |
| 102 | + password: 'secure_password', |
| 103 | + ssl: { |
| 104 | + rejectUnauthorized: true, |
| 105 | + ca: '-----BEGIN CERTIFICATE-----\n...', |
| 106 | + }, |
| 107 | +}; |
| 108 | +``` |
| 109 | + |
| 110 | +### Connection Pool Configuration |
| 111 | + |
| 112 | +```typescript |
| 113 | +const config: PostgresConfig = { |
| 114 | + database: 'myapp', |
| 115 | + host: 'localhost', |
| 116 | + port: 5432, |
| 117 | + username: 'postgres', |
| 118 | + password: 'secret', |
| 119 | + max: 20, // Max 20 connections |
| 120 | + min: 5, // Keep at least 5 connections |
| 121 | + idleTimeoutMillis: 30000, // Close idle connections after 30s |
| 122 | + connectionTimeoutMillis: 2000, // Timeout connection attempts after 2s |
| 123 | + statementTimeout: 5000, // Abort slow queries after 5s |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## Related |
| 130 | + |
| 131 | +- [Driver](/docs/references/data/driver) - Base driver protocol |
| 132 | +- [Driver SQL](/docs/references/data/driver-sql) - SQL driver interface |
| 133 | +- [Datasource](/docs/references/system/datasource) - Datasource configuration |
0 commit comments