CodeIgniter Forums
Configuring the Database Connection in CodeIgniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: Configuring the Database Connection in CodeIgniter 4 (/showthread.php?tid=92962)



Configuring the Database Connection in CodeIgniter 4 - Witimpat - 05-27-2025

Hi all,
After installing CodeIgniter 4, the next important step is setting up your database connection. Here’s how you do it:
  • Open
    Code:
    app/Config/Database.php
    file.
  • Update the
    Code:
    $default
    group settings with your database credentials (hostname, username, password, database name).
  • Make sure the database driver matches your DBMS (e.g., MySQLi for MySQL).
  • Save the file and test your connection by running a simple model query.
If you want to use environment variables for better security, CodeIgniter supports that too! Just edit the
Code:
.env
file at the root and set your DB credentials there.
Let me know if you want me to share sample code snippets or troubleshoot errors.
Cheers!