First of all, the problem is about Laravel not postgresql or PHP. I can connect postgresql with a simple PHP file. But Laravel can’t do it somehow.
When I try to connect postgresql server in my computer with laravel I get “PDOException with message ‘could not find driver'” error. I am getting this error when I run DB::connection()->getPdo();
command at artisan tinker.
If I run php artisan migrate
command, the error is IlluminateDatabaseQueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')
My configuration is below:
- Windows 10
- Wamp Server 3.1.4
- Apache 2.4.35
- PHP 7.2.10
- Laravel Framework 6.0.3
Related lines of Laravel .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_dnm1
DB_USERNAME=postgres
DB_PASSWORD=mrd.BE.265
Related lines of Laravel database.php file:
'default' => env('DB_CONNECTION', 'pgsql'),
…
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
When I run print_r(PDO::getAvailableDrivers());
at my server I get below output:
Array ( [0] => mysql [1] => pgsql [2] => sqlite )
Related lines of php info is below:
NOTE: There is no problem when I use mysql instead of postgresql.
NOTE2: I can connect the DB when I use regular PHP. Only Laravel gives this error.
4
Answers
I solved the issue. It is a bug at WAMP I think. When I edit php.ini from WAMP's menu it opens a different php.ini than active PHP version's. So I opened php.ini from file system and edited it from there.
Verify your PHP version with
php -v
Install
php7.2-pgsql
when needed.install postgresql
then uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file
then restart apache2
There are different php version installed on your WAMP SERVER. when you click on WAMP icon it will show that pgsql and pdo_pgsql are active but for different php version. You need to check your php version using php -v command and then go to the WAMP folder and find that php version and edit php.ini file and enable pgsql extensions there.