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