pgdb

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: BSD-3-Clause-Clear Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreatePoolConfig

func CreatePoolConfig(cfg *database.PostgresConfig) (*pgxpool.Config, error)

CreatePoolConfig converts database.PostgresConfig to pgxpool.Config This is the secure way to create a config without exposing passwords in DSN strings

func FindConfig added in v0.7.0

func FindConfig(configFiles []string) (*database.PostgresConfig, string, error)

FindConfig searches for and parses the first existing config file. Returns the PostgresConfig, the path to the config file used, and any error. If DATABASE_URI env var is set, returns nil config with empty path (use ParseURIConfig instead).

func GetConfigFiles added in v0.7.0

func GetConfigFiles() []string

GetConfigFiles returns the list of config files to search for database configuration. Checks DATABASE_CONFIG_FILE env var first, otherwise returns default paths.

func OpenPool

func OpenPool(ctx context.Context, options PoolOptions) (*pgxpool.Pool, error)

OpenPool opens a native pgx connection pool with the specified configuration This is the primary and recommended way to connect to PostgreSQL

Configuration precedence (highest to lowest): 1. DATABASE_URI environment variable (pool settings can be included in URI) 2. DATABASE_CONFIG_FILE environment variable (YAML) 3. Default config files (database.yaml, /vault/secrets/database.yaml)

When using DATABASE_URI, pool settings (pool_max_conns, pool_min_conns, etc.) can be specified in the URI query string and PoolOptions are ignored. When using config files, PoolOptions are applied.

func OpenPoolWithConfigFile

func OpenPoolWithConfigFile(ctx context.Context, configFile string) (*pgxpool.Pool, error)

OpenPoolWithConfigFile opens a connection pool using an explicit config file path This is a convenience function for when you have a specific config file

func ParseURIConfig added in v0.7.0

func ParseURIConfig() (*pgxpool.Config, error)

ParseURIConfig extracts connection info from DATABASE_URI environment variable. Returns nil if DATABASE_URI is not set.

Types

type PoolOptions

type PoolOptions struct {
	// ConfigFiles is a list of config file paths to search for database configuration
	ConfigFiles []string

	// MinConns is the minimum number of connections in the pool.
	// Default: 0 (matches pgxpool default)
	MinConns int32

	// MaxConns is the maximum number of connections in the pool.
	// Default: 4 (matches pgxpool default)
	// For higher concurrency, increase via PoolOptions or URI ?pool_max_conns=N
	MaxConns int32

	// MaxConnLifetime is the maximum lifetime of a connection.
	// Default: 1 hour (matches pgxpool default)
	MaxConnLifetime time.Duration

	// MaxConnIdleTime is the maximum idle time of a connection.
	// Default: 30 minutes (matches pgxpool default)
	MaxConnIdleTime time.Duration

	// HealthCheckPeriod is how often to check connection health.
	// Default: 1 minute (matches pgxpool default)
	HealthCheckPeriod time.Duration
}

PoolOptions configures pgxpool connection behavior.

Default values match pgxpool defaults from github.com/jackc/pgx/v5/pgxpool. See: https://pkg.go.dev/github.com/jackc/pgx/v5/pgxpool#Config

To customize pool settings, either:

  • Modify PoolOptions before calling OpenPool (for config file mode)
  • Use URI query parameters like ?pool_max_conns=25 (for DATABASE_URI mode)

func DefaultPoolOptions

func DefaultPoolOptions() PoolOptions

DefaultPoolOptions returns defaults matching pgxpool. See https://pkg.go.dev/github.com/jackc/pgx/v5/pgxpool#Config for pgxpool defaults.

Jump to

Keyboard shortcuts

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