Back to Error Center

No database selected

Error Message

SQLSTATE[3D000]: Invalid database name or no database selected

Cause

The database connection doesn't have a database specified, or the database name in .env is incorrect.

Solution

Check DB_DATABASE in .env. Ensure the database exists on the server. Verify the database connection configuration in config/database.php.

Code Example

Fix
# Check .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app  # Must match an existing database
DB_USERNAME=root
DB_PASSWORD=

# Create database if it doesn\'t exist
php artisan db:create

Debugging Steps

  1. Check DB_DATABASE in .env
  2. Verify the database exists on the server
  3. Check config/database.php for connection settings
  4. Test connection with a database client

Prevention

Always verify database exists before running migrations. Use php artisan db:create to create databases. Include database setup in deployment scripts.