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-mysqlgithub.com/netlifeguru/db-postgresgithub.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(), nilCommon Fields
These fields are shared across supported drivers.
| Field | Purpose |
|---|---|
Identifier | Logical name for the connection pool |
Host | Database host |
Port | Database port |
Database | Database name or Scylla keyspace |
Username | Database username |
Password | Database password |
MaxConns | Maximum number of open connections |
MinConns | Minimum number of idle or retained connections |
MaxConnIdleTime | Maximum time an idle connection may remain open |
MaxConnLifetime | Maximum lifetime of a connection |
HealthCheckPeriod | Driver health check interval where supported |
ConnectTimeout | Timeout 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:
| Driver | Default port |
|---|---|
| MySQL | 3306 |
| Postgres | 5432 |
| Scylla | 9042 |
If Port is not set, the driver uses its own default.
Database
Database identifies the selected database or keyspace.
Database: "app",Meaning by driver:
| Driver | Meaning |
|---|---|
| MySQL | Database name |
| Postgres | Database name |
| Scylla | Keyspace |
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.
| Field | MySQL | Postgres | Scylla |
|---|---|---|---|
SSLMode | yes | yes | no |
TimeZone | yes | yes | no |
Consistency | no | no | yes |
SSLMode
SSLMode controls TLS or SSL behavior for SQL drivers.
SSLMode: "disable",Common values:
| Driver | Common values |
|---|---|
| MySQL | false, true, skip-verify, custom TLS config name |
| Postgres | disable, require, verify-ca, verify-full |
Examples:
SSLMode: "false", // MySQL local developmentSSLMode: "disable", // PostgreSQL local developmentSSLMode: "require", // PostgreSQL or CockroachDB-style secure connectionTimeZone
TimeZone configures the connection timezone where supported.
TimeZone: "UTC",Typical values:
| Driver | Common value |
|---|---|
| MySQL | Local |
| Postgres | UTC |
Examples:
TimeZone: "Local",TimeZone: "UTC",Consistency
Consistency is used by the Scylla driver.
Consistency: "local_quorum",Common values:
| Value | Meaning |
|---|---|
one | One replica must respond |
quorum | A quorum of replicas must respond |
local_quorum | A quorum in the local datacenter must respond |
all | All 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.
| Field | MySQL | PostgreSQL | Scylla |
|---|---|---|---|
Identifier | default | default | default |
Host | 127.0.0.1 | 127.0.0.1 | 127.0.0.1 |
Port | 3306 | 5432 | 9042 |
MaxConns | 25 | 25 | 25 |
MinConns | 2 | 2 | 2 |
MaxConnIdleTime | 5m | 5m | 5m |
MaxConnLifetime | 1h | 1h | 1h |
ConnectTimeout | 5s | 5s | 5s |
HealthCheckPeriod | 30s | 30s | 30s |
SSLMode | false | disable | - |
TimeZone | Local | UTC | - |
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.