Basic Structure
database
Specifies the PostgreSQL driver to use. Type:"pg" | "pgnative" (optional)
Default: "pg"
pg vs pgnative
-
"pg": PostgreSQL driver implemented in pure JavaScript (recommended)- Install:
pnpm add pg - Cross-platform support
- Sufficient performance for most cases
- Sonamu automatically configures Node.js TCP socket-level keepAlive (
keepAlive: true,keepAliveInitialDelayMillis: 10000)
- Install:
-
"pgnative": Native driver using C bindings- Install:
pnpm add pg-native - Faster performance (especially for large data processing)
- Requires compilation, platform-dependent
- Sonamu automatically converts the connection object to a libpq connection string and includes TCP keepAlive parameters (
keepalives=1,keepalives_idle=10,keepalives_interval=10,keepalives_count=5)
- Install:
name
Specifies the database name. Type:string (required)
defaultOptions
Database settings that apply to all environments. Uses Knex configuration options. Type:DatabaseConfig (required)
connection Settings
Sets database connection information.Additional Knex Options
You can set various Knex options besides connection:| Option | Default | Description |
|---|---|---|
propagateCreateError | false | Does not propagate connection creation errors to the entire pool |
idleTimeoutMillis | 10000 | Idle connections are automatically released after 10 seconds |
reapIntervalMillis | 1000 | Idle connection cleanup interval is set to 1 second |
acquireTimeoutMillis | 30000 | Connection acquisition wait time is limited to 30 seconds |
createTimeoutMillis | 30000 | Connection creation wait time is limited to 30 seconds |
afterCreate callback registers socket-level keepAlive settings and error handlers for each connection. Connections that encounter errors are automatically excluded from the pool.
environments
Specifies different database settings for each environment. OverridesdefaultOptions.
Type: (optional)
Environment-Specific Configuration Example
Determining Current Environment
Sonamu determines the current environment in this order:NODE_ENVenvironment variable- Default:
development
Slave DB Configuration
When using read-only replicas (slave):Slave DB is used for read-only queries to distribute load from the master DB.
Practical Examples
Basic Local Development Setup
Using Environment Variables (Recommended)
Multi-Environment Configuration
Docker Compose Environment
Connection Testing
To verify your database settings are correct:Important Notes
1. Password Security
2. Port Number Type
3. Connection Pool Size
Next Steps
After completing database settings:- server - Server options and plugin settings
- database/migrations - Manage schema with migrations
- troubleshooting - Connection problem troubleshooting