postgres

package
v0.18.4 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(cfg Config) (*sqlx.DB, error)

Connect creates a connection to the Postgres instance.

func Setup

func Setup(cfg Config, migrations migrate.MemoryMigrationSource) (*sqlx.DB, error)

Setup creates a connection to the Postgres instance and applies any unapplied database migrations. A non-nil error is returned to indicate failure.

Types

type Config

type Config struct {
	Host        string     `env:"HOST"           envDefault:"localhost"`
	Port        string     `env:"PORT"           envDefault:"5432"`
	User        string     `env:"USER"           envDefault:"supermq"`
	Pass        string     `env:"PASS"           envDefault:"supermq"`
	Name        string     `env:"NAME"           envDefault:""`
	SSLMode     string     `env:"SSL_MODE"       envDefault:"disable"`
	SSLCert     string     `env:"SSL_CERT"       envDefault:""`
	SSLKey      string     `env:"SSL_KEY"        envDefault:""`
	SSLRootCert string     `env:"SSL_ROOT_CERT"  envDefault:""`
	Pool        PoolConfig `envPrefix:"POOL_"`
}

Config defines the options that are used when connecting to a TimescaleSQL instance.

type PoolConfig added in v0.17.0

type PoolConfig struct {
	// MaxConnLifetime is the duration since creation after which a connection will be automatically closed.
	MaxConnLifetime time.Duration `env:"MAX_CONN_LIFETIME" envDefault:"1h"`

	// pool_max_conn_lifetime_jitter
	MaxConnLifetimeJitter time.Duration `env:"MAX_CONN_LIFETIME_JITTER" envDefault:"0"`

	// MaxConnIdleTime is the duration after which an idle connection will be automatically closed by the health check.
	MaxConnIdleTime time.Duration `env:"MAX_CONN_IDLE_TIME" envDefault:"15m"`

	// MaxConnLifetime is the duration since creation after which a connection will be automatically closed.
	MaxConns uint16 `env:"MAX_CONNS" envDefault:"5"`

	// MinConns is the minimum size of the pool. After connection closes, the pool might dip below MinConns. A low
	// number of MinConns might mean the pool is empty after MaxConnLifetime until the health check has a chance
	// to create new connections.
	MinConns uint16 `env:"MIN_CONNS" envDefault:"1"`

	// MinIdleConns is the minimum number of idle connections in the pool. You can increase this to ensure that
	// there are always idle connections available. This can help reduce tail latencies during request processing,
	// as you can avoid the latency of establishing a new connection while handling requests. It is superior
	// to MinConns for this purpose.
	// Similar to MinConns, the pool might temporarily dip below MinIdleConns after connection closes.
	MinIdleConns uint16 `env:"MIN_IDLE_CONNS" envDefault:"1"`

	// HealthCheckPeriod is the duration between checks of the health of idle connections.
	HealthCheckPeriod time.Duration `env:"HEALTH_CHECK_PERIOD" envDefault:"1m"`
}

Jump to

Keyboard shortcuts

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