Documentation
¶
Overview ¶
Package postgres provides pgxpool creation with configurable connection limits and retry.
Configuration ¶
Config holds the connection URL and optional pool limits. URL is required (e.g. postgres://user:pass@host:5432/db?sslmode=disable). MaxConns and MinConns default to 10 and 0 when zero (so when only MinConns is set, MaxConns is still 10); MaxConns must be in 1..10000 when set, MinConns in 0..10000, and MinConns <= MaxConns. RetryTimeout overrides the default 30s window for connection retry. Do not log Config as-is; String and GoString mask the password in the URL. MaskURL(s) is a standalone helper for safe logging of any connection string. Error messages from New also mask the URL when reporting parse failures.
Creating a pool ¶
New creates a pgxpool.Pool with exponential backoff until the database is reachable (or ctx is cancelled). Pool settings (MaxConnLifetime, MaxConnIdleTime, HealthCheckPeriod) use sensible defaults. Use the returned pool for queries and call Close when shutting down.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
URL string // PostgreSQL connection URL (required). Password is masked in String/GoString.
MaxConns int // Max connections in pool; 0 uses default (10). When set must be 1..10000. Overrides URL.
MinConns int // Min idle connections; 0 uses default (0). Must be 0..10000 and <= MaxConns. Overrides URL.
RetryTimeout time.Duration // Max time for connection retry; 0 uses default (30s).
MaxConnLifetime time.Duration // Max lifetime of a connection; 0 uses default (1h).
MaxConnIdleTime time.Duration // Max idle time of a connection; 0 uses default (30m).
HealthCheckPeriod time.Duration // How often to check connection health; 0 uses default (15s).
ConnectTimeout time.Duration // Timeout for establishing a connection; 0 uses default (5s).
}
Config holds connection settings for New. String and GoString mask the password; do not log raw Config. MaxConns and MinConns override any pool parameters in the URL (e.g. pool_max_conns); when 0, defaults are used. Duration fields use zero for library defaults (MaxConnLifetime 1h, MaxConnIdleTime 30m, HealthCheckPeriod 15s, ConnectTimeout 5s).