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

dreamcatcher's avatar

Run Migrate Command With Differen SQL User

I would like change sql user privilages on production environment (revoke alter privilages) but if I will change privileges, migration command will not work properly because user does not have alter privilages. There is any method for run migration under different user?

1 like
3 replies
kmjadeja's avatar

Hello @dreamcatcher

There is one way to do this I'm not sure that this method is good or not.

Steps :

  • Create new connection with same database but different user
  • When you migrate you can select connection php artisan migrate --database=YOUR_DB_NAME
  • By doing this all the table will migrated to your database with given user's privileges.

Thanks!

2 likes
JeffJones's avatar

@kmjadeja This seems like the most practical solution. I typically use an underprivileged user for normal application database access. And then have a privileged user for situations that require it.

You can also add a property to each of the migrations that require the privileged user. https://laravel.com/docs/11.x/migrations#setting-the-migration-connection

That way you won't need to use the --database parameter for artisan migrate.

It also allows you to pick and choose which user (connection) you need for any give migration.

This is especially useful when using multiple databases in an application.


Update: I found that setting the connection property in the migration files breaks testing since the property apparently overrides any test settings. So my suggestion does not work.

Sinnbeck's avatar

Another solution that does not require editing database.php

Create a secondary env file with the escalated credentials. migration.env

When you deploy

php artisan config:clear 
php artisan migrate --env=migration 
php artisan config:cache

Please or to participate in this conversation.