➺  Mervin Villaceran

How to use Phinx for database migration in a Shared Hosting using CodeIgniter 3

February 1, 2021 1 min read
PHP CodeIgniter 3 Phinx

Most shared hosting servers have no access to ssh which is hard for a developer to migrate a database. But with Symfony console application, we can run our migration commands inside our web application.

What is Phinx?

Phinx is a portable open-source migration library in PHP. It will help you to write, manage and migrate your database easily to the different database vendors. See full documentation of Phinx here.


In your CodeIgniter 3 project, install phinx via composer.

composer require robmorgan/phinx

Then update your phinx config file.
See phinx installation here for more info.

Create new controller

Create a new controller and make sure that only the admin can access this controller if you don't want others to run the migration.

Next, import Phinx console application and symfony console component:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class Database extends CI_Controller
public function __construct()

Create migration function

Lastly, create a function that will be use to run the migrations.
You can also add a parameter to specify the environment if you have different environments in your shared hosting.

public function update(string $environment)
// init Phinx console application
$phinx = new PhinxApplication();

// create new Symfony console input
$input = new ArrayInput(array(
'command' => 'migrate',
'-c' => FCPATH . 'phinx.php', // path to your config file
'-e' => $environment, // either testing, development, production, etc.

$output = new BufferedOutput();
$phinx->run($input, $output);

// display output to user
echo json_encode(nl2br($output->fetch()));

There you have it. You can now access the controller in your browser to run your database migrations.

Keep coding!