NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

AboutMulti-DriverSQL Files
ConfigPools
DBConnections

Config

Configure database connections using the shared db.Config structure.

The db.Config struct is shared by all NetLifeGuru database drivers.

It describes how a driver should create and configure a database connection or connection pool.

The same struct is used by:

  • github.com/netlifeguru/db-mysql
  • github.com/netlifeguru/db-postgres
  • github.com/netlifeguru/db-scylla

Not every field is used by every driver. Some options are common, while others are driver-specific.

Config Structure

type Config struct {
	Identifier string

	Host     string
	Port     int
	Database string
	Username string
	Password string

	SSLMode  string
	TimeZone string

	MaxConns          int32
	MinConns          int32
	MaxConnIdleTime   time.Duration
	HealthCheckPeriod time.Duration
	MaxConnLifetime   time.Duration
	ConnectTimeout    time.Duration

	Consistency string
}

Basic Example

cfg := db.Config{
	Identifier: "default",

	Host:     "127.0.0.1",
	Port:     3306,
	Database: "app",
	Username: "root",
	Password: "secret",

	MaxConns:          50,
	MinConns:          5,
	MaxConnIdleTime:   10 * time.Minute,
	MaxConnLifetime:   2 * time.Hour,
	HealthCheckPeriod: 30 * time.Second,
	ConnectTimeout:    10 * time.Second,
}

Pass the config to the selected driver:

conn := mysql.New()

if err := conn.CreatePool(cfg); err != nil {
	return err
}

After the pool is created, use Fork to get a db.Conn for application code.

return conn.Fork(), nil

Common Fields

These fields are shared across supported drivers.

FieldPurpose
IdentifierLogical name for the connection pool
HostDatabase host
PortDatabase port
DatabaseDatabase name or Scylla keyspace
UsernameDatabase username
PasswordDatabase password
MaxConnsMaximum number of open connections
MinConnsMinimum number of idle or retained connections
MaxConnIdleTimeMaximum time an idle connection may remain open
MaxConnLifetimeMaximum lifetime of a connection
HealthCheckPeriodDriver health check interval where supported
ConnectTimeoutTimeout used when creating or validating the connection

Identifier

Identifier is the logical name of the connection pool.

Identifier: "default"

Use identifiers when your application manages multiple pools.

Examples:

Identifier: "default"
Identifier: "analytics"
Identifier: "tenant-a"
Identifier: "tenant-b"

The same identifier cannot be reused with a different configuration.

If an existing pool already uses the same identifier but with different connection settings, the driver returns a pool identifier conflict error.

Host and Port

Host and Port define where the database server is located.

Host: "127.0.0.1",
Port: 3306,

Default ports:

DriverDefault port
MySQL3306
Postgres5432
Scylla9042

If Port is not set, the driver uses its own default.

Database

Database identifies the selected database or keyspace.

Database: "app",

Meaning by driver:

DriverMeaning
MySQLDatabase name
PostgresDatabase name
ScyllaKeyspace

For Scylla, Database should contain the keyspace name.

Database: "app_keyspace",

Username and Password

Use Username and Password for database authentication.

Username: "app_user",
Password: "secret",

Drivers pass these values to their underlying database client.

For local development, these values are usually loaded from environment variables.

Username: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),

Pool Settings

Pool settings control how many connections can be opened and how long they are reused.

MaxConns:          50,
MinConns:          5,
MaxConnIdleTime:   10 * time.Minute,
MaxConnLifetime:   2 * time.Hour,
HealthCheckPeriod: 30 * time.Second,

MaxConns

MaxConns defines the maximum number of open connections.

MaxConns: 50,

Use a value that matches your application workload and database capacity.

MinConns

MinConns defines the minimum number of retained or idle connections where supported by the driver.

MinConns: 5,

If MinConns is greater than MaxConns, drivers normalize it to avoid invalid pool configuration.

MaxConnIdleTime

MaxConnIdleTime defines how long an idle connection can remain open.

MaxConnIdleTime: 10 * time.Minute,

MaxConnLifetime

MaxConnLifetime defines the maximum lifetime of a connection.

MaxConnLifetime: 2 * time.Hour,

This is useful for rotating long-lived connections.

HealthCheckPeriod

HealthCheckPeriod defines how often the driver should check connection health where supported.

HealthCheckPeriod: 30 * time.Second,

ConnectTimeout

ConnectTimeout defines how long the driver waits when opening or validating a connection.

ConnectTimeout: 10 * time.Second,

Driver-Specific Fields

Some fields are only used by specific drivers.

FieldMySQLPostgresScylla
SSLModeyesyesno
TimeZoneyesyesno
Consistencynonoyes

SSLMode

SSLMode controls TLS or SSL behavior for SQL drivers.

SSLMode: "disable",

Common values:

DriverCommon values
MySQLfalse, true, skip-verify, custom TLS config name
Postgresdisable, require, verify-ca, verify-full

Examples:

SSLMode: "false",   // MySQL local development
SSLMode: "disable", // PostgreSQL local development
SSLMode: "require", // PostgreSQL or CockroachDB-style secure connection

TimeZone

TimeZone configures the connection timezone where supported.

TimeZone: "UTC",

Typical values:

DriverCommon value
MySQLLocal
PostgresUTC

Examples:

TimeZone: "Local",
TimeZone: "UTC",

Consistency

Consistency is used by the Scylla driver.

Consistency: "local_quorum",

Common values:

ValueMeaning
oneOne replica must respond
quorumA quorum of replicas must respond
local_quorumA quorum in the local datacenter must respond
allAll replicas must respond

Use the consistency level that matches your Scylla data model and availability requirements.

MySQL Config Example

cfg := db.Config{
	Identifier: "default",

	Host:     os.Getenv("DB_HOST"),
	Port:     3306,
	Database: os.Getenv("DB_NAME"),
	Username: os.Getenv("DB_USER"),
	Password: os.Getenv("DB_PASSWORD"),

	MaxConns:          50,
	MinConns:          5,
	MaxConnIdleTime:   10 * time.Minute,
	MaxConnLifetime:   2 * time.Hour,
	HealthCheckPeriod: 30 * time.Second,
	ConnectTimeout:    10 * time.Second,

	SSLMode:  "false",
	TimeZone: "Local",
}

PostgreSQL Config Example

cfg := db.Config{
	Identifier: "default",

	Host:     os.Getenv("DB_HOST"),
	Port:     5432,
	Database: os.Getenv("DB_NAME"),
	Username: os.Getenv("DB_USER"),
	Password: os.Getenv("DB_PASSWORD"),

	MaxConns:          50,
	MinConns:          5,
	MaxConnIdleTime:   10 * time.Minute,
	MaxConnLifetime:   2 * time.Hour,
	HealthCheckPeriod: 30 * time.Second,
	ConnectTimeout:    10 * time.Second,

	SSLMode:  "disable",
	TimeZone: "UTC",
}

Scylla Config Example

cfg := db.Config{
	Identifier: "default",

	Host:     os.Getenv("DB_HOST"),
	Port:     9042,
	Database: os.Getenv("DB_NAME"), // Scylla keyspace
	Username: os.Getenv("DB_USER"),
	Password: os.Getenv("DB_PASSWORD"),

	MaxConns:          50,
	MinConns:          5,
	MaxConnIdleTime:   10 * time.Minute,
	MaxConnLifetime:   2 * time.Hour,
	HealthCheckPeriod: 30 * time.Second,
	ConnectTimeout:    10 * time.Second,

	Consistency: "local_quorum",
}

Defaults

Each driver applies defaults when optional fields are not provided.

FieldMySQLPostgreSQLScylla
Identifierdefaultdefaultdefault
Host127.0.0.1127.0.0.1127.0.0.1
Port330654329042
MaxConns252525
MinConns222
MaxConnIdleTime5m5m5m
MaxConnLifetime1h1h1h
ConnectTimeout5s5s5s
HealthCheckPeriod30s30s30s
SSLModefalsedisable-
TimeZoneLocalUTC-
Consistency--quorum

Recommended Usage

Load connection settings from environment variables.

cfg := db.Config{
	Identifier: "default",
	Host:       os.Getenv("DB_HOST"),
	Database:   os.Getenv("DB_NAME"),
	Username:   os.Getenv("DB_USER"),
	Password:   os.Getenv("DB_PASSWORD"),
}

Then add driver-specific options only when needed.

For MySQL:

cfg.SSLMode = "false"
cfg.TimeZone = "Local"

For PostgreSQL:

cfg.SSLMode = "disable"
cfg.TimeZone = "UTC"

For Scylla:

cfg.Consistency = "local_quorum"

Keep the same db.Config shape across the application, but let each driver interpret the fields it supports.

Scylla

Create a ScyllaDB connection using the NetLifeGuru Scylla driver and the shared DB layer.

Pools

Understand connection pools, identifiers, shared pools, Fork, and Close behavior across DB drivers.

On this page

Config StructureBasic ExampleCommon FieldsIdentifierHost and PortDatabaseUsername and PasswordPool SettingsMaxConnsMinConnsMaxConnIdleTimeMaxConnLifetimeHealthCheckPeriodConnectTimeoutDriver-Specific FieldsSSLModeTimeZoneConsistencyMySQL Config ExamplePostgreSQL Config ExampleScylla Config ExampleDefaultsRecommended Usage