Environment Variables
The simplest way to configure your database is through the.env file:
Connection String Formats
| Database | Format | Example |
|---|---|---|
| SQLite | sqlite:<path> | sqlite:./database.db |
| PostgreSQL | postgres://user:pass@host:port/db | postgres://admin:secret@localhost:5432/myapp |
| MySQL | mysql://user:pass@host:port/db | mysql://root:password@localhost:3306/myapp |
DatabaseConfig
For programmatic configuration, useDatabaseConfig:
Builder Pattern
You can also build configuration manually:Connection Pooling
Kit automatically manages a connection pool for optimal performance. Configure pooling settings throughDatabaseConfig:
| Setting | Default | Description |
|---|---|---|
max_connections | 10 | Maximum number of connections in the pool |
min_connections | 1 | Minimum idle connections to maintain |
connect_timeout_seconds | 30 | Timeout for acquiring a connection |
idle_timeout_seconds | 600 | Time before idle connections are closed |
Database Initialization
The database is automatically initialized when your Kit application starts. TheDB::init() method is called during bootstrap:
Manual Initialization
If you need to initialize the database manually:Accessing the Connection
Use theDB facade to access the database connection anywhere in your application:
Multiple Databases
For applications requiring multiple database connections, you can manage connections manually:Environment-Specific Configuration
Use different configurations for development, testing, and production:Troubleshooting
Connection Refused
If you see connection refused errors:- Verify the database server is running
- Check the connection string format
- Ensure network access (firewall, security groups)
Pool Exhausted
If connections are being exhausted:- Increase
max_connections - Ensure connections are being released (avoid long-running transactions)
- Check for connection leaks in your code