postgresdb

package
v0.2.3 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// DefaultMaxOpenConns is the default maximum number of open connections.
	// Conservative default: 4 database pools × 5 = 20 total connections,
	// well under PostgreSQL's default max_connections of 100.
	DefaultMaxOpenConns = 5
	// DefaultMaxIdleConns is the default maximum number of idle connections.
	// Set equal to MaxOpenConns to avoid connection churn.
	DefaultMaxIdleConns = 5
	// DefaultConnMaxLifetime is the default maximum connection lifetime.
	// 30 minutes provides stability while still recycling stale connections.
	DefaultConnMaxLifetime = 30 * time.Minute
)

Variables

This section is empty.

Functions

func New

func New(connURL string, schema string, opts ...Option) (*sql.DB, error)

New creates a new PostgreSQL database connection. The connURL should be a PostgreSQL connection string in the format: postgres://user:password@host:port/dbname?sslmode=disable If schema is provided, a separate PostgreSQL schema will be created and used.

Types

type Option

type Option func(*Options)

Option is a functional option for configuring PostgreSQL connections.

func OptionsFromConfig

func OptionsFromConfig(cfg app.PostgresConfig) []Option

OptionsFromConfig creates functional options from app.PoolConfig. Returns nil if cfg is nil. Zero values in cfg use the defaults.

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) Option

WithConnMaxLifetime sets the maximum connection lifetime.

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns sets the maximum number of idle connections.

func WithMaxOpenConns

func WithMaxOpenConns(n int) Option

WithMaxOpenConns sets the maximum number of open connections.

type Options

type Options struct {
	// MaxOpenConns is the maximum number of open connections to the database.
	MaxOpenConns int
	// MaxIdleConns is the maximum number of idle connections in the pool.
	MaxIdleConns int
	// ConnMaxLifetime is the maximum amount of time a connection may be reused.
	ConnMaxLifetime time.Duration
}

Options configures a PostgreSQL connection.

Jump to

Keyboard shortcuts

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