About Database Forge Class of CodeIgniter 4 Tutorial

Reading Time: 5 minutes

The Database Forge class in CodeIgniter 4 provides a powerful collection of tools for working with database structures and schema alterations. The Database Forge class allows you to conduct a wide range of database operations within the CodeIgniter framework, such as creating tables, updating columns, adding indexes, and much more.

In this in-depth lesson, we will look at the Database Forge class in CodeIgniter 4. We will walk you through the step-by-step method of using this class to easily construct and alter database structures.

Read More: Cron Job Task Scheduling In Laravel 10 Example Tutorial

The Database Forge class will be your go-to tool whether you need to create a new database from start or make changes to an existing schema.

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

What is Database Forge Class?

The Database Forge class in CodeIgniter 4 provides a strong set of tools for modifying database structures and schema.

It contains methods that help you manage your database.

Load the Forge Class as follows:

$forge = \Config\Database::forge();

Read More: Laravel 10 Collection push() and put() Methods Tutorial

If the database you wish to manage is not the default one, you may alternatively supply another database group name to the DB Forge loader:

$this->forge = \Config\Database::forge('other_db');

How To Create Database Using Forge Object

We can use createDatabase() method of database forge to create database.

Here, is the call to create it:


It returns true/false based on success or failure.

If the optional second parameter is set to true, an IF EXISTS statement will be added or a database will be checked for existence before being created (depending on the DBMS).

$forge->createDatabase('myDb', true);

How To Drop Database Using Forge Object

To drop a schema, you can use dropDatabase() method of it.

Here, is the way to call it.


It also returns true/false based on success or failure.

How To Create Table Using Forge Object

When building tables, you may want to do a few things. Change the columns, add fields, and add keys to the table. CodeIgniter has a way for doing this.

Read More: CodeIgniter 4 How to Print or Get Last Executed Query

We will use these following methods to create a table with defined fields and also with keys.





Here, is the complete code by which you can create a table structure in database.


namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreatePostsTable extends Migration
    public function up()
            "id" => [
                "type" => "INT",
                "constraint" => 5,
                "auto_increment" => true,
                "unsigned" => true,
            "title" => [
                "type" => "VARCHAR",
                "constraint" => 80,
                "null" => false,
            "content" => [
                "type" => "TEXT",
                "null" => true,
            "created_at datetime default current_timestamp",
            "updated_at datetime default current_timestamp",


    public function down()

We hope this article helped you to learn about About Database Forge Class of CodeIgniter 4 Tutorial in a very detailed way.

Read More: Convert Image To The Base64 String Using Javascript

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.