Overview
Ironclad supports multiple database systems with flexible configuration options. The framework uses environment variables for configuration and provides compile-time checked database connections through SQLx.Environment Configuration
Database settings are configured through environment variables in your.env file:
Configuration Structure
Ironclad uses strongly-typed configuration structs defined insrc/config/mod.rs:
PostgreSQL Configuration
MongoDB Configuration
Loading Configuration
Configuration is loaded from environment variables using theAppConfig::from_env() method:
Connection Pool Settings
Max Connections
The maximum number of database connections in the pool:- Development: 5-10 connections
- Production: Based on your database server capacity and application load
- Ensure your database server can handle the configured number of connections
Min Connections
The minimum number of idle connections to maintain:Acquire Timeout
Maximum time (in seconds) to wait for a connection from the pool:Idle Timeout
Maximum time (in seconds) a connection can remain idle before being closed:Connection Pool Initialization
Here’s how Ironclad initializes the PostgreSQL connection pool:Database URL Format
PostgreSQL
MongoDB
Multiple Database Support
Ironclad can work with multiple database systems simultaneously. TheAppConfig struct includes optional database configurations:
MONGODB_URL is set in the environment.
Troubleshooting
Connection Refused
Error:Connection refused (os error 111)
Solutions:
- Verify the database server is running
- Check the host and port in
DATABASE_URL - Ensure firewall rules allow the connection
- For Docker, use the container name instead of
localhost
Too Many Connections
Error:FATAL: sorry, too many clients already
Solutions:
- Reduce
DB_MAX_CONNECTIONSvalue - Increase the database server’s max connections limit
- Check for connection leaks in your code
Acquire Timeout
Error:timed out while waiting for an open connection
Solutions:
- Increase
DB_ACQUIRE_TIMEOUTvalue - Increase
DB_MAX_CONNECTIONSto handle more concurrent requests - Optimize slow queries that hold connections
- Check for connections not being properly returned to the pool
Invalid Configuration
Error:Failed to load configuration
Solutions:
- Ensure
.envfile exists in the project root - Verify all required environment variables are set
- Check for invalid values (e.g., non-numeric values for numeric fields)
- Use
.env.exampleas a reference for required variables
SSL Connection Issues
Error:SSL error: certificate verify failed
Solutions:
- For development, add
?sslmode=disableto yourDATABASE_URL - For production, ensure proper SSL certificates are configured
- Use
?sslmode=requireto enforce SSL in production
Best Practices
- Never commit
.envfiles: Use.env.examplefor documentation - Use different credentials per environment: Development, staging, and production should have separate database credentials
- Monitor connection pool usage: Watch for connection pool exhaustion in production
- Set appropriate timeouts: Balance between user experience and resource usage
- Enable SSL in production: Always use encrypted connections for production databases
- Use connection pooling: SQLx’s built-in pooling is production-ready and efficient
- Validate configuration on startup: The framework validates configuration when loading from environment
Next Steps
PostgreSQL Setup
Learn about PostgreSQL-specific features and usage
Database Migrations
Set up and manage SQLx migrations