18

I’m working on Laravel 5 with postgres as database. I’ve configured postgres 9.4 and pgAdmin III and these are working normally. When I try to run migrate it is giving me error:

[PDOException]
could not find driver

This is my database.php

'default' => 'pgsql',

'pgsql' => [            'driver'   => 'pgsql',          
                        'host'     => '127.0.0.1',          
                        'database' => 'fms',            
                        'username' => 'postgres',           
                        'password' => 'root',           
                        'charset'  => 'utf8',           
                        'prefix'   => '',           
                        'schema'   => 'public',         ],

Initially I though, it was due to configuration of postgres on windows 7 but I tried with plain php it works perfect

<?php
   $host        = "host=127.0.0.1";
   $port        = "port=5432";
   $dbname      = "dbname=fms";

   $db = pg_connect( "$host $port $dbname user=postgres password=root"  );
   if(!$db){
      echo "Error : Unable to open database\n";
   } else {
      echo "Opened database successfully\n";
   }
?>

I’ve enabled php_pgsql and php_pdo_sql in wamp as well. I’m not sure how to fix this on laravel 5.

Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57

7 Answers7

38

As you said you already choosed Default Database as Postgres SQL

'default' => 'pgsql',

It is a must that you need to uncomment the pdo and postgres shared object in your php configuration settings (php.ini)

i.e., You need to uncomment the following lines in your php.ini

extension=pdo_pgsql.so
extension=pgsql.so

Note :

Don't forget to stop and start your apache after doing this changes (or php-fpm if using that instead).

wp78de
  • 18,207
  • 7
  • 43
  • 71
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
  • I ran `sudo apt install php7.4-pgsql` https://www.php.net/manual/en/pgsql.installation.php#119801 and then `service php7.4-fpm restart` but I still get "Unable to load dynamic library 'pdo_pgsql'" – Ryan Jun 10 '20 at 20:03
8

I had the same problem with Laravel-WAMP-PostgreSql driver not found exception. I even successfully established direct connection to Postgre as you have, but with no luck with the "php artisan migrate" command.

After long research I found out that there are multiple php.ini files in which "extension=php_pdo_pgsql.dll" and "extension=php_pgsql.dll" are comented out.

The solution is (of course) to uncoment the extensions in the following files:

  • ~/wamp/bin/php/php5.5.*/php.ini
  • ~/wamp/bin/php/php5.5.*/phpForApache
  • ~/wamp/bin/php/php5.5.*/php.ini.install
  • ~/wamp/bin/php/php5.5.*/php.ini-development
  • ~/wamp/bin/php/php5.5.*/php.ini-production and
  • ~/wamp/bin/apache/apache2.4.9/php.ini

** You can leave off the "php.ini-development" and "php.ini-production" (don't need to uncoment these files).

anchor
  • 755
  • 2
  • 8
  • 17
3

Like @jeff said, this is probably caused by not setting DB_CONNECTION=pgsql in the .env-file. The .env-file has MySQL preconfigured, so you must edit this file.

nichoio
  • 6,289
  • 4
  • 26
  • 33
3

You have to make DB related changes in

  1. config/database.php
  2. .env file

and other settings in

  • php.ini settings

If you are still getting error, then clear cache and config

php artisan cache:clear
php artisan config:clear

It should work now!

Ravistm
  • 2,163
  • 25
  • 25
  • 1
    I stopped the command running 'php artisan:serve' and then I run your commands and again run 'php artisan:serve' and that worked!. Thanks very much. – sebasdev Jan 28 '19 at 17:45
  • I had everything right, I was 100% sure cause I tried using external scripts, the part of clearing cache and config did it! thanks – Hawili May 17 '19 at 14:15
1

Run this command to easily uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so from php.ini

sed -ri -e 's!;extension=pdo_pgsql!extension=pdo_pgsql!' $PHP_INI_DIR/php.ini

sed -ri -e 's!;extension=pgsql!extension=pgsql!' $PHP_INI_DIR/php.ini

If you do not have the drivers already installed, or it gives error like unable to load dynamic library PGSQL run this:

apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pgsql pdo_pgsql

This will install the pgsql and pdo_pgsql drivers.

Ash
  • 672
  • 6
  • 21
0

you gotta modify .env file , then go to config>database.php and change the default to pgsql, and also in all your models file you need to change $protected $connection= 'pgsql'.

Jeff
  • 9
  • 5
-1

I'm using PHP7.3 and this worked for me, I forgot this:

apt-get install php7.3-pgsql

Everything runs smoothly afterwards. Just use whatever version of PHP you are using.