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

stratboy's avatar

How to force the database name when installing Laravel?

Hi, it seems that the db name when installing laravel (both composer installation and laravel.build/sail way) is not configurable. Is there a way to not having always a 'laravel' database? I'd like to assign a custom name.

0 likes
4 replies
LaryAI's avatar
Level 58

To force a custom database name when installing Laravel, you can follow these steps:

  1. Create a new Laravel project: If you haven't already created a new Laravel project, you can do so using Composer or Laravel Sail. For example:

    composer create-project --prefer-dist laravel/laravel my-project
    
  2. Configure the .env file: After creating the project, you need to configure the .env file to use your custom database name. Open the .env file in the root of your project and update the DB_DATABASE value to your desired database name. For example:

    DB_DATABASE=my_custom_database
    
  3. Update the config/database.php file: Laravel uses the config/database.php file to manage database connections. Ensure that the database key in the mysql connection array is set to env('DB_DATABASE', 'laravel'). This ensures that the value from the .env file is used.

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'laravel'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
    
  4. Create the database: Ensure that the database with the custom name exists. You can create it using a database management tool like phpMyAdmin, or via the command line. For example, using MySQL:

    mysql -u root -p
    CREATE DATABASE my_custom_database;
    
  5. Run the migrations: Finally, run the Laravel migrations to set up the database schema.

    php artisan migrate
    

By following these steps, you can configure Laravel to use a custom database name during installation and setup.

Tray2's avatar

That functionality has not yet been added to the installer, but you are free to add it to the installer code, or just write a shell script for it. I used this code in a shell script to update the database name in the .env.

function updateEnv() {
  readonly APP_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${1:0:1})${1:1}"
  echo 'Updating the .env file to fit your project.'
  if [ $POSTGRES = true ]
  then
      sed -i -e "s/DB_CONNECTION=mysql/DB_CONNECTION=pgsql/g" .env > /dev/null 2>&1
      sed -i -r "s/DB_PORT=3306/DB_PORT=5432/g" .env > /dev/null 2>&1
  fi
  sed -i -e "s/DB_DATABASE=laravel/DB_DATABASE=$DATABASE_NAME/g" .env > /dev/null 2>&1
  sed -i -e "s/APP_NAME=Laravel/APP_NAME=$APP_NAME/g" .env > /dev/null 2>&1
  rm .env-e > /dev/null 2>&1
  echo '.env updated.'
}

This line changes the database name.

 sed -i -e "s/DB_DATABASE=laravel/DB_DATABASE=$DATABASE_NAME/g" .env > /dev/null 2>&1
stratboy's avatar

@martinbean Hi, after the Lary answer (I didn't see yours), I realised I couldn't simply rename the db AFTER installation. So here I was asking if I can force the name DURING installation. I thought it was some way possible. It felt quite strange to me it wasn't.

Anyway, thank you for the other answer. To say the truth, I already tried that way before asking, and it didn't worked. But I trust you, so I probably did something wrong and I'm going to try again. Otherwise, I see that probably I just have to learn to play with the database right in terminal. I should already have learned it, but I'm basically a php/wordpress and frontend programmer, I never really had to, always used guis. Now it's time. :)

Please or to participate in this conversation.