Database Configuration
⏱ Estimated reading time: 2 min
Database Configuration in CodeIgniter (CodeIgniter 4)
CodeIgniter provides a simple and flexible way to configure and manage database connections using configuration files and environment variables.
1. Supported Databases
CodeIgniter 4 supports:
-
MySQL / MariaDB
-
PostgreSQL
-
SQLite
-
SQL Server
-
Oracle
2. Database Configuration Location
You can configure the database in two ways:
-
Using
.envfile (Recommended) -
Using
app/Config/Database.php
3. Database Configuration Using .env (Recommended)
Rename the env file:
Edit .env:
✔ More secure
✔ Easy to change per environment
4. Database Configuration Using Database.php
File:
Example:
5. Setting Environment Mode
In .env:
This enables:
-
Detailed database error messages
-
Debugging support
6. Connecting to the Database
Automatic Connection (Model)
Manual Connection
7. Testing Database Connection
Create a controller method:
8. Multiple Database Connections
Add another configuration:
Connect:
9. Database Prefix
Table name:
10. Enable Query Logging
In Database.php:
Logs stored in:
11. Security Best Practices
✅ Use .env for credentials
✅ Disable DBDebug in production
❌ Never commit .env to Git
Summary
-
Configure DB using
.envorDatabase.php -
Supports multiple databases
-
Secure & environment-based setup
-
Easy connection via models or services
Register Now
Share this Post
← Back to Tutorials