postgres

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 7 Imported by: 0

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

func MaskURL

func MaskURL(s string) string

MaskURL masks the password in a PostgreSQL connection URL for safe logging. On parse error uses regex fallback so that userinfo (including passwords containing @) is fully masked.

func New

func New(ctx context.Context, cfg *Config) (*pgxpool.Pool, error)

New creates a pgxpool with exponential backoff until the database is reachable. ctx can cancel the retry. Returns an error if ctx or cfg is nil, the URL is invalid, or pool limits are out of range.

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).

func (Config) GoString

func (c Config) GoString() string

GoString implements fmt.GoStringer with the URL password masked. Use for %#v in logs.

func (Config) String

func (c Config) String() string

String returns a string representation of the config with the URL password masked. Use for logging.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL