Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zain's avatar
Level 1

php artisan migrate command generate data in multiple schema's

i want to run migration for multiple schema's using pgsql.

Please help !

Thanks

0 likes
13 replies
christopher's avatar

Just put all your shema`s in your migration file. Just as an example:

public function up()
    {
        Schema::create('activations', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('code');
            $table->boolean('completed')->default(0);
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();

            $table->engine = 'InnoDB';
        });

        Schema::create('persistences', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('code');
            $table->timestamps();

            $table->engine = 'InnoDB';
            $table->unique('code');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('activations');
        Schema::drop('persistences');
    }

}
1 like
zain's avatar
Level 1

Thanks hostianer! Hmm .. But actually i am working on multi-tenancy. I have multiple schema's in single db. for example PostgreSQL db has public,private1,private2 schema's. I want to run migration in all schema's by running "php artisan migrate"

bobbybouwmann's avatar

When you run php artisan migrate it loops over all yourr migration files and performs them. So I don't see any issue with running that...?

zain's avatar
Level 1

Thanks blackbird! as i said i want to migrate all files for all schemas in "pgsql" database. i am working on multi tenant application in which i need to run migrations for all schema's using "php artisan migrate" .

Please help!

bobbybouwmann's avatar

So first go to config/database.php and update this 'default' => 'mysql' to this 'default' => 'pgsql'. The next step is to make sure that you have the correct settings in connections array in the same file.

Now go to the terminal and you can generate a migration with this:

php artisan make:migration create_posts_table

and for another table

php artisan make:migration create_categories_table

Now when you run php artisan migrate both tables are created in the database

1 like
zain's avatar
Level 1

Thanks blackbird! but how these commands will migrate files in multiple schema's of pgsql ?

Valorin's avatar

Give this a try:

In your config/database.php file, duplicate the pgsql section in connections so you have one for each of your schemas.

Then inside your migrations, reference each connection specifically:

Schema::connection('connectionOne')->create('tableOne', function(Blueprint $table)
{
    // ...
});

Schema::connection('connectionTwo')->create('tableOne', function(Blueprint $table)
{
    // ...
});

I don't know anything about how the different schema's in PostgreSQL work, but I assume you can just connect to each independently, and then just reference each connection explicitly in Laravel?

1 like
zain's avatar
Level 1

that's something static .... what if i want to migrate file in multiple schema's by getting all schemas from pgsql db and then migrate one by one for all schemas ?

thepsion5's avatar

If you have a common naming scheme for your databases, you can iterate over all of them presumably. I'm not 100% sure how you'd do this in pgsql, but in MySQL you could do something like:

  1. Execute a statement that would return a list of databases starting with "private"

  2. Retrieve that list as an array

  3. Loop through the list and run the migration for each

I'd create a special migration class that would contain the logic for retrieving the list of databases and iterating over them so you don't have to worry about it in your individual migration files. Something like:

abstract class MultiTenantMigration extends Illuminate\Database\Migrations\Migration
{
    protected function getSchemas($startingWith = 'private')
    {
        $results = DB::getPdo()->query('SHOW DATABASES')->fetchColumn();
        return array_filter($results, function($item) use($startingWith)
        {
            return (strpos($item, $startingWith) === 0);
        }
    }

    public function run()
    {
        $databases = $this->getSchemas();
        Config::set('database.default_connection', 'current_tenant');
        foreach($databases as $database) {
            Config::set('database.connections.current_tenant', array( /* details, including database name */) );;
            $this->runMigration();
        }
    }

    protected abstract function runMigration();
}

I'm not sure how well this would work in practice, but I have any major reasons to believe it wouldn't.

2 likes
zain's avatar
Level 1

Thanks thepsion5 ... i will try it.

daylight's avatar

Did this work? I am running into this same issue for a multi-tenant application using PostGRES. It would be nice if Laravel would officially address this and add in support for multi-tenant/multi-schema setups.

osymo's avatar

@zain i'm facing the same problem right now with pgsql schemas, did someone find a solution ??

shiro_'s avatar

For me, the schema name was saved in a table called $facility so i looped through all facilities and set the search path to be the schema name in my migration file. My code looks like this :

 public function up()
    {
        Schema::table('diseases', function (Blueprint $table) {

            $facilities = Facility::all();
        
            foreach($facilities as $facility) {

            $search_path = $facility['schema_name'];

            DB::statement("SET search_path = $search_path");

                Schema::table('diseases', function (Blueprint $table) {

                    $table->integer('disease_category_id')->nullable();

                });

            }
        });
    }

Please or to participate in this conversation.