CodeIgniter provides a simple and flexible way to configure and manage database connections using configuration files and environment variables.
CodeIgniter 4 supports:
MySQL / MariaDB
PostgreSQL
SQLite
SQL Server
Oracle
You can configure the database in two ways:
Using .env file (Recommended)
Using app/Config/Database.php
.env (Recommended)Rename the env file:
Edit .env:
✔ More secure
✔ Easy to change per environment
Database.phpFile:
Example:
In .env:
This enables:
Detailed database error messages
Debugging support
Create a controller method:
Add another configuration:
Connect:
Table name:
In Database.php:
Logs stored in:
✅ Use .env for credentials
✅ Disable DBDebug in production
❌ Never commit .env to Git
Configure DB using .env or Database.php
Supports multiple databases
Secure & environment-based setup
Easy connection via models or services
Take quizzes related to this topic and see where you stand!
Start Quiz Now