Skip to content

Database (pgx)

pkg/db/drivers/pgx manages a PostgreSQL connection pool using jackc/pgx.

lakta.NewRuntime(
config.NewModule(
config.WithConfigDirs(".", "./config"),
config.WithArgs(os.Args[1:]),
),
tint.NewModule(),
slog.NewModule(),
pgx.NewModule(),
myapp.NewModule(),
)
func (m *MyModule) Init(ctx context.Context) error {
pool := do.MustInvoke[*pgxpool.Pool](lakta.GetInjector(ctx))
m.pool = pool
return nil
}
pgx.NewModule(pgx.WithName("primary")),
pgx.NewModule(pgx.WithName("replica")),
modules:
db:
pgx:
primary:
dsn: "postgres://user:pass@primary:5432/mydb"
replica:
dsn: "postgres://user:pass@replica:5432/mydb"

Config path: modules.db.pgx.<name>

dsn required string

dSN is the database connection string used to configure the database connection

env LAKTA_MODULES__DB__PGX__<NAME>__DSN
max_open_conns int32 default: 10

maxOpenConns specifies the maximum number of open connections to the database. It maps to the "max_open_conns" configuration

env LAKTA_MODULES__DB__PGX__<NAME>__MAX_OPEN_CONNS
log_level string default: info

logLevel specifies the logging level for database operations, supporting values like trace, debug, info, warn, error, none

env LAKTA_MODULES__DB__PGX__<NAME>__LOG_LEVEL
health_check bool

healthCheck enables or disables the database health check mechanism

env LAKTA_MODULES__DB__PGX__<NAME>__HEALTH_CHECK
min_conns int32

minConns is the minimum number of idle connections kept in the pool

env LAKTA_MODULES__DB__PGX__<NAME>__MIN_CONNS
max_conn_lifetime time.Duration default: 1h0m0s

maxConnLifetime is the maximum age of a connection before it is closed

env LAKTA_MODULES__DB__PGX__<NAME>__MAX_CONN_LIFETIME
max_conn_idle_time time.Duration default: 30m0s

maxConnIdleTime is the maximum idle time before a connection is closed

env LAKTA_MODULES__DB__PGX__<NAME>__MAX_CONN_IDLE_TIME
health_check_period time.Duration default: 1m0s

healthCheckPeriod is how often the pool checks idle connection health

env LAKTA_MODULES__DB__PGX__<NAME>__HEALTH_CHECK_PERIOD
statement_timeout time.Duration default: 30s

statementTimeout sets the per-statement timeout (Postgres statement_timeout). Zero disables it

env LAKTA_MODULES__DB__PGX__<NAME>__STATEMENT_TIMEOUT