Lumen 5.6连接PostgreSQL提示could not find driver问题求助
Hey there, let's work through this frustrating issue together. Since your PostgreSQL connection works fine in native PHP and Laravel 5.6, the problem is almost certainly tied to how Lumen is interacting with your PHP environment or its own configuration. Here are the steps to diagnose and fix it:
1. Verify PHP PostgreSQL Extensions Are Enabled (and Lumen is using the right PHP instance)
First, double-check that the PostgreSQL extensions are actually enabled for the PHP environment Lumen is using. Sometimes CLI and web server environments use different php.ini files:
- Run
php -min your terminal to list loaded modules. Look forpdo_pgsqlandpgsql—both should be present. - Create a simple
phpinfo.phpfile in your Lumen public directory with<?php phpinfo(); ?>, access it via your browser, and search for the same extensions. Ensure they match the CLI output.
If either environment is missing the extensions, you'll need to enable them:
- For Ubuntu/Debian: Run
sudo apt-get install php-pgsql(adjust for your PHP version, e.g.,php7.1-pgsqlfor PHP 7.1). - For Windows: Uncomment
extension=pdo_pgsqlandextension=pgsqlin yourphp.inifile. - Restart your web server (Apache/Nginx) and CLI session after making changes.
2. Ensure Lumen is Loading Your Database Configuration Correctly
Lumen is lighter than Laravel, so it doesn't include a default config/database.php file. If you haven't already, create this file in your Lumen project's config directory with the PostgreSQL configuration:
<?php return [ 'default' => env('DB_CONNECTION', 'pgsql'), 'connections' => [ 'pgsql' => [ 'driver' => 'pgsql', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 5432), 'database' => env('DB_DATABASE', 'test'), 'username' => env('DB_USERNAME', 'xxxx'), 'password' => env('DB_PASSWORD', 'xxxxxx'), 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', 'sslmode' => 'prefer', ], ], ];
This ensures Lumen has the full PostgreSQL connection details, just like Laravel does.
3. Clear Any Stale Configuration Cache
If you've ever run php artisan config:cache in your Lumen project, it might be holding onto old configuration values. Delete the bootstrap/cache/config.php file (if it exists) to force Lumen to read fresh configuration from your .env and config/database.php files.
4. Test a Direct PDO Connection in Lumen
To rule out issues with Lumen's database layer, create a quick test route in routes/web.php to connect directly via PDO:
Route::get('/test-pdo', function () { try { $pdo = new PDO( 'pgsql:host=' . env('DB_HOST') . ';port=' . env('DB_PORT') . ';dbname=' . env('DB_DATABASE'), env('DB_USERNAME'), env('DB_PASSWORD') ); return "Direct PDO connection successful! 🎉"; } catch (PDOException $e) { return "PDO Connection Error: " . $e->getMessage(); } });
Access this route in your browser. If it fails, the problem is with your PHP environment (extensions or path). If it works, the issue is with Lumen's database service setup.
5. Reinstall Composer Dependencies
Sometimes corrupted or mismatched dependencies can cause issues. Run these commands to refresh your dependencies:
composer dump-autoload composer install
This ensures all Laravel/Lumen database components are properly installed and compatible with your version.
Final Checks
- Confirm your
.envfile is in the root of your Lumen project (not in a subdirectory) and that theDotenvloader inbootstrap/app.phpis pointing to the correct path (your current bootstrap.php looks correct here). - Make sure Lumen has permission to read the
.envfile (check file permissions if you're on a Unix-based system).
Go through these steps one by one, and you should be able to get your Lumen PostgreSQL connection working just like Laravel does!
内容的提问来源于stack exchange,提问作者Sivabalan

