Create a PostgreSQL pool
Edit this pageClient · Related pages
Posted: 2026-09-16 · Report this thread
NewPostgresPool(ctx context.Context, user string, password string, host string, database string, cfg *PostgresConnectionConfig) (*pgxpool.Pool, error)Builds a pgx pool from connection parameters. It does not test connectivity.
NewClient pings the pool when creating a SQLStreams client. The caller owns
and closes the pool.
Example
Use the application’s database credentials. This example reads the password
from PGPASSWORD.
pool, err := sqlstreams.NewPostgresPool(ctx, "app_user", os.Getenv("PGPASSWORD"), "localhost", "app_db", &sqlstreams.PostgresConnectionConfig{MaxConns: 20})if err != nil { return err}defer pool.Close()Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx |
context.Context |
Yes | Context passed to pool construction. |
user |
string |
Yes | Database role. |
password |
string |
Yes | Role password. An empty string supplies no password. |
host |
string |
Yes | Database host, without the port. IPv6 addresses are supported. |
database |
string |
Yes | Database name. |
cfg |
*PostgresConnectionConfig |
No | nil uses defaults. The supplied struct has its default Port filled before validation. |
PostgresConnectionConfig
| Field | Type | Default | Description |
|---|---|---|---|
Port |
int |
5432 |
Database port. Must be positive. |
MaxConns |
int |
0 |
A positive value sets the pool limit. Zero leaves pgx’s default, the greater of 4 and the CPU count. Must be nonnegative. |
ConnectTimeout |
time.Duration |
0 |
Maximum time to establish a connection. Zero leaves pgx’s connection configuration unchanged. Must be nonnegative. |
TLSConfig |
*tls.Config |
nil |
Optional TLS configuration assigned to the parsed pgx connection config. nil preserves pgx’s negotiation. Use a pgx DSN when configuring sslmode and fallback behavior explicitly. |
Returns
| Condition | Return value |
|---|---|
| Pool construction succeeds | (pool, nil). This does not establish that credentials or connectivity work. |
| Parameters or pool configuration are invalid | (nil, err). |
Existing pools and connection URLs
NewClient accepts any *pgxpool.Pool. An application with an existing pool
can pass it directly. For a connection URL, construct the pool with
pgxpool.New(ctx, dsn) and pass that pool to NewClient.
Close the pool after producers, consumers, and other database work have stopped.
Errors
| Error | When |
|---|---|
| Validation error | User, host, or database is empty, or connection configuration is invalid. |
| Pool configuration error | pgx rejects the generated connection configuration. |
Related topics
- Create a client — Test connectivity and configure the SQLStreams installation.