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

stratboy's avatar

How to rename the database after sail/docker installation?

Hi, I just

curl -s "https://laravel.build/example-app" | bash

But I'm not happy with the default dateabase name, which is 'laravel'. Updating the .env and database.php files is not enough since the new database does not exists in docker. So maybe I can create another one or just rename the existing one. I guess the latter is the best. But I don't know how. I tried to sail down and then sail build --no-cache but it doesn't work. How to?

0 likes
5 replies
LaryAI's avatar
Level 58

To rename the database after setting up Laravel Sail/Docker, you can follow these steps:

  1. Update the .env file: Change the DB_DATABASE value to your desired database name.

    DB_DATABASE=new_database_name
    
  2. Update the config/database.php file: Ensure that the database key under the mysql connection matches the new database name.

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'new_database_name'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        // other configurations...
    ],
    
  3. Access the MySQL container: Use Laravel Sail to access the MySQL container.

    sail exec mysql bash
    
  4. Log in to MySQL: Once inside the MySQL container, log in to MySQL using the root user or the user specified in your .env file.

    mysql -u root -p
    

    Enter the password when prompted.

  5. Rename the database: Inside the MySQL shell, you can rename the database. Unfortunately, MySQL does not support a direct RENAME DATABASE command, so you will need to create a new database and move the data.

    CREATE DATABASE new_database_name;
    
  6. Copy data to the new database: You can use the mysqldump command to export the data from the old database and import it into the new one.

    mysqldump -u root -p old_database_name > old_database_name.sql
    mysql -u root -p new_database_name < old_database_name.sql
    
  7. Drop the old database: After ensuring that the data has been successfully copied, you can drop the old database.

    DROP DATABASE old_database_name;
    
  8. Restart Sail: Exit the MySQL container and restart Sail to apply the changes.

    exit
    sail down
    sail up -d
    

By following these steps, you should be able to rename your database in a Laravel Sail/Docker environment.

martinbean's avatar

@stratboy You need to change the name of the DB_DATABASE variable in your .env file and re-run sail build --no-cache to rebuild the Docker containers from scratch (including the database one with your new database name).

stratboy's avatar

@martinbean Hi Martin, I tried again to run sail build --no-cache after renaming the DB in .env file, but no luck:

.env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=cercasemi
DB_USERNAME=sail
DB_PASSWORD=password

From Docker mysql exec:

sh-4.4# mysql -uroot -p
Enter password: 
[...]
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| laravel            |
| mysql              |
| performance_schema |
| sys                |
| testing            |
+--------------------+

No 'cercasemi' here.

Tray2's avatar

@stratboy Of course not, you can run the migration and it will ask you if you want to create it.

stratboy's avatar

@Tray2 I ended up creating it by hand from terminal, far easier.

Please or to participate in this conversation.