Laravel提示Database [postgres]未配置,附配置文件求解决
Hey there, let's get this connection issue sorted out for you. Looking at your configs, I can spot a couple of straightforward fixes that should resolve the error:
1. Mismatched Connection Name in database.php
Your database.php sets 'default' => 'postgres', but your PostgreSQL connection configuration is nested under the 'pgsql' key. Laravel looks for a connection entry that exactly matches the default name you specify—right now it's searching for a 'postgres' key that doesn't exist, hence the error.
Fix:
Stick to Laravel's default convention by updating the default value to match the connection key:
'default' => env('DB_CONNECTION', 'pgsql'),
Alternatively, you could rename the connection key to 'postgres' to match your current default setting, but the first option is more consistent with how Laravel is set up out of the box.
2. Incorrect Environment Variable Reference
In your pgsql connection config, you have 'username' => env('postgres', '')—this is trying to pull an environment variable named postgres, which isn't defined in your .env file (your username variable is DB_USERNAME).
Fix:
Update the username (and double-check your password line too) to reference the correct .env variables:
'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''),
3. Clear Configuration Cache (Important!)
After making these changes, clear Laravel's config cache to ensure the new settings are loaded instead of cached old values:
php artisan config:clear
Once you apply these tweaks, your PostgreSQL connection should work without that error popping up.
内容的提问来源于stack exchange,提问作者Lyes

