how to change database name in laravel? changing config>database.php doesn't work
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
The Mystery of the Misbehaving Database: Why Changing config/database.php Isn't Enough in Laravel
As senior developers, we often encounter situations where configuration seems to be the simple fix, but the underlying behavior remains stubbornly incorrect. Dealing with database connectivity issues in frameworks like Laravel can feel like debugging a phantom—the settings look right, but the application still connects to the wrong place.
This post dives into a common frustration: trying to change the database name within config/database.php and expecting Laravel's Eloquent or Query Builder to immediately follow suit. We’ll diagnose why this happens and establish the correct principles for managing database connections in a robust Laravel application.
The Frustration: Configuration vs. Execution
Let's examine the scenario you described. You successfully updated your config/database.php file, pointing the connection details to your desired database (mydatabase). However, when executing queries using the DB facade or Eloquent, Laravel seems to default back to an older connection context (like homestead) instead of respecting the new configuration.
This discrepancy highlights a crucial distinction in software development: Configuration defines how to connect, but the execution environment and underlying networking define where we actually connect.
When you use methods like DB::table('user'), Laravel is executing raw SQL through PHP's PDO layer. This layer relies heavily on the operating system's network stack and DNS resolution to establish an actual TCP connection. If the hostname specified in your configuration (e.g., mysite.local) doesn't resolve correctly, or if the execution environment (like Docker containers managed by Homestead) prioritizes a different default host, the PDO driver defaults to whatever connection it thinks is valid, often leading to unexpected results like accessing the old database name (homestead.user).
Diagnosing the Root Cause: Hostnames and Environment
The core issue in your case appears to be environmental—specifically related to how your local development environment (like Homestead or Docker) manages hostname resolution versus what Laravel explicitly requests.
When you change the host in database.php, you are telling PHP where to attempt the connection. If that host is misconfigured, or if the system defaults to resolving a different IP for that hostname, the connection fails silently at the application layer, and the resulting error points back to the default context rather than indicating a direct configuration failure.
The subsequent PDOException (2002) SQLSTATE[HY000] [2002] Connection refused when switching to localhost confirms this: the updated configuration was being read, but the actual network handshake failed because the assumed host wasn't reachable by the PHP process at that moment.
The Correct Approach: Mastering Database Abstraction
Instead of fighting against environment quirks, we should lean into Laravel’s built-in abstraction layers to manage connections explicitly. For robust applications, especially when dealing with multi-environment setups or complex networking, relying directly on raw connection strings in the config file can introduce these types of ambiguity.
Best Practice: Explicit Connection Management
If you frequently switch between database environments (e.g., local development vs. staging), managing these connections explicitly is safer than relying solely on hostnames that are prone to environmental shifts.
For complex setups, consider using environment variables extensively, or if you need to manage multiple distinct connection types, utilize the DB::connection() method. This gives you direct control over which defined connection profile Laravel should use for a specific query, bypassing potential confusion in the default settings.
As we explore advanced data handling and structure within the framework, understanding these underlying mechanics is key to building scalable solutions on platforms like Laravel.
Conclusion
The lesson here is that database configuration in Laravel is not just a static file change; it’s an interaction between the application code, the PHP PDO driver, and the host operating system's networking stack. When configuration changes don't yield immediate results, pause and investigate the environmental layer. Always check your DNS resolution, network settings, and container configurations before assuming the framework itself is at fault. By focusing on explicit connection management and solid environment setup, you ensure that your Laravel application interacts with the database exactly as intended, regardless of the surrounding infrastructure.