Postgres pool
Edit this pagePosted: 2026-09-09 · Report this thread
sqlstreams.NewPostgresPool builds a *pgxpool.Pool from the parts of a
Postgres URL, in the order the URL writes them:
postgres://user:password@host:port/database. It does not dial; NewClient
is what pings the pool, so a wrong password surfaces there, not here.
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()
The password goes inline, "" meaning no password, which is real on a trust
or peer auth setup. A password holding @ or # and an IPv6 host survive:
the DSN is built through net/url.
Config
PostgresConnectionConfig
| field | default | what it decides |
|---|---|---|
Port | 5432 | the port |
MaxConns | pgx’s own, max(4, numCPU) | the pool’s size when > 0; raise it for high worker counts |
ConnectTimeout | pgx’s own (none) | mirrors pgconn.Config.ConnectTimeout |
TLSConfig | pgx’s own negotiation (sslmode=prefer) | mirrors pgconn.Config.TLSConfig; the package never sets sslmode itself |
Schema is not here. It was never a connection fact and sits on
ClientConfig.
Gotchas
- A caller holding a DSN skips this entirely:
pgxpool.New(ctx, dsn)and hand the result toNewClient, which takes any*pgxpool.Pool. That is the path for aDATABASE_URLdeployment and for an application whose pool exists before SQLStreams enters it. - Whichever way the pool is built, you close it.
defer pool.Close()is the only shutdown this layer needs. - Taking a pool commits the public API to pgx. A team on
database/sqlruns one pool: build the pgxpool, hand it to SQLStreams, and derive the*sql.DBview with pgx’s ownstdlib.OpenDBFromPool(pool), which shares the pool and leaves closing to you. The derivation runs only that direction, so a team that will not import pgx runs a second pool beside SQLStreams’s. - A pool you built can carry its own
search_path, on the DSN as?search_path=myappor in anAfterConnecthook. SQLStreams does not care: every statement it runs names its schema outright.