DSN Format
PostgreSQL connection strings use thepostgres:// or postgresql:// protocol:
Basic Examples
Connection Options
SSL/TLS Modes
PostgreSQL supports three SSL modes via thesslmode query parameter:
Timeouts
Configure connection and query timeouts via TOML:pg library (src/connectors/postgres/index.ts:70-79).
Read-Only Mode
PostgreSQL supports SDK-level read-only enforcement usingdefault_transaction_read_only:
- INSERT, UPDATE, DELETE
- CREATE, ALTER, DROP
- TRUNCATE, COPY
Schema Search Path
Configure the default schema search path:getTables()without schema parametergetTableSchema()without schema parametergetStoredProcedures()without schema parameter
Supported Features
Schemas
PostgreSQL has full schema support. List all schemas:public (or first schema from search_path config)
Tables and Views
Get tables in a schema:Table Schema with Comments
PostgreSQL supports column comments for documentation:Indexes
PostgreSQL indexes include:- Primary keys (
is_primary: true) - Unique indexes (
is_unique: true) - Multi-column indexes
- Partial indexes
- Expression indexes
Stored Procedures and Functions
PostgreSQL distinguishes between procedures (PROCEDURE) and functions (FUNCTION):
Row Count Estimation
For large tables, PostgreSQL provides fast row count estimates usingpg_class.reltuples:
COUNT(*) for large tables and provides reasonable accuracy after ANALYZE.
Table Comments
Retrieve table-level comments:Query Execution
Parameterized Queries
PostgreSQL uses$1, $2, etc. for parameters:
pg library for safe, prepared execution.
Multi-Statement Support
PostgreSQL executes multiple statements within a transaction for consistency:Row Limiting
Applies database-native LIMIT clause for efficiency:Common Scenarios
Connect to Local PostgreSQL
Connect to Cloud PostgreSQL (AWS RDS, Azure, GCP)
Connect Through SSH Tunnel
Use Multiple Schemas
Read-Only Analytics Connection
PostgreSQL-Specific SQL Examples
JSON Queries
Full-Text Search
Window Functions
CTEs (Common Table Expressions)
Troubleshooting
Connection Refused
Error:connect ECONNREFUSED 127.0.0.1:5432
Solutions:
- Verify PostgreSQL is running:
pg_isready - Check
postgresql.confforlisten_addresses - Verify
pg_hba.confallows your connection method - Check firewall rules
Authentication Failed
Error:password authentication failed for user "username"
Solutions:
- Verify username and password
- Check
pg_hba.confauthentication method (md5, scram-sha-256) - Try connecting with
psqlto verify credentials
SSL Required
Error:no pg_hba.conf entry for host, SSL off
Solution: Add ?sslmode=require to your DSN:
Timeout Issues
Error:Connection timeout or Query timeout
Solutions:
- Increase timeouts in TOML configuration
- Check network connectivity
- Optimize slow queries
- Consider using connection pooling
Schema Not Found
Error:schema "myschema" does not exist
Solutions:
- Verify schema exists:
SELECT schema_name FROM information_schema.schemata; - Check search_path configuration
- Use fully qualified table names:
schema.table
Performance Tips
- Use read replicas for analytics queries to reduce load on primary
- Enable connection pooling (enabled by default in connector)
- Set appropriate timeouts to prevent long-running queries from blocking
- Use EXPLAIN ANALYZE to optimize slow queries
- Leverage row count estimation for large tables instead of COUNT(*)
- Configure search_path to avoid schema qualification in queries