5
votes

I am having a hard time setting up laravel 5.3 (running on wamp/windows) with pgsql. I am getting an error "PDO Exception - Could not find driver" when I am trying to migrate tables with "php artisan migrate"

  1. I installed postgres 9.6.
  2. I enabled the php extensions for postgresql:

enter image description here 3. I added postgres libraries to environment path variable

enter image description here

  1. I tested the pdo postgres connection with php and I am getting connected:


    $dbh = new PDO("pgsql:dbname=rotocms;host=127.0.0.1", "postgres", "mypassword");

    if($dbh){
     echo "connected";
    }else{
     echo 'there has been an error connecting';
    }


So I am stuck, cant get the migration to work in laravel. My .env:


    DB_CONNECTION=pgsql
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_DATABASE=mydatabasename
    DB_USERNAME=postgres
    DB_PASSWORD=password

Here is the laravel error stack:

    local.ERROR: PDOException: could not find driver in C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:119
Stack trace:
#0 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(119): PDO->__construct('pgsql:host=127....', 'postgres', 'password', Array)
#1 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(56): Illuminate\Database\Connectors\Connector->createPdoConnection('pgsql:host=127....', 'postgres', 'password', Array)
#2 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\PostgresConnector.php(36): Illuminate\Database\Connectors\Connector->createConnection('pgsql:host=127....', Array, Array)
#3 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(100): Illuminate\Database\Connectors\PostgresConnector->connect(Array)
#4 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
#5 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(964): call_user_func(Object(Closure))
#6 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(832): Illuminate\Database\Connection->getPdo()
#7 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(717): Illuminate\Database\Connection->reconnectIfMissingConnection()
#8 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php(350): Illuminate\Database\Connection->run('select * from i...', Array, Object(Closure))
#9 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Schema\PostgresBuilder.php(25): Illuminate\Database\Connection->select('select * from i...', Array)
#10 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Migrations\DatabaseMigrationRepository.php(156): Illuminate\Database\Schema\PostgresBuilder->hasTable('migrations')
#11 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php(515): Illuminate\Database\Migrations\DatabaseMigrationRepository->repositoryExists()
#12 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php(92): Illuminate\Database\Migrations\Migrator->repositoryExists()
#13 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php(58): Illuminate\Database\Console\Migrations\MigrateCommand->prepareDatabase()
#14 [internal function]: Illuminate\Database\Console\Migrations\MigrateCommand->fire()
#15 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Container\Container.php(508): call_user_func_array(Array, Array)
#16 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Console\Command.php(169): Illuminate\Container\Container->call(Array)
#17 C:\wamp64\www\myproject\vendor\symfony\console\Command\Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Console\Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 C:\wamp64\www\myproject\vendor\symfony\console\Application.php(820): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 C:\wamp64\www\myproject\vendor\symfony\console\Application.php(187): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Migrations\MigrateCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 C:\wamp64\www\myproject\vendor\symfony\console\Application.php(118): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(121): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 C:\wamp64\www\myproject\artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 {main}  

Also PostgreSQL (libpq) Version in the phpinfo() is 9.4.4 even though i installed PostgreSQL 9.6 not sure if that is ok..

3
Make sure you've uncommented the line that tells php where the Postgres driver is (usually extension=pgsql.so) in the main php.ini file, take a look at this - stackoverflow.com/questions/10085039/…Saumya Rastogi
I am on windows and I posted my phpinfo postgresql part. the lines in my php.ini are uncommented: extension=php_pdo_pgsql.dll extension=php_pgsql.dllAdnan Mujkanovic
Have you write database name user password in both .env and database.php fileWasiq Muhammad
yes i tried that, restarted everything but still not working. still same error. :(Adnan Mujkanovic

3 Answers

0
votes

Under config\database.php it is a file name database.php

Here Select default to pgsql like this

'default' => env('DB_CONNECTION', 'pgsql')

Here modify this with all your details

'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ]

Restart your server hopefully it connects

0
votes

You have to look for a couple of things,

Make sure to configure the 'default' key in app/config/database.php

For postgres, this would be 'default' => 'pgsql',

Check if you have the correct PHP extensions installed. You need pdo_pgsql.so and pgsql.so installed and enabled. Instructions on how to do this vary between operating systems.

Uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so in php.ini

Now copy libpq.dll from C:\wamp\bin\php\php5.*\ into C:\wamp\bin\apache*\bin and restart all services.

If the issue persists the look into your Environment Variables.

Let me know the results.

0
votes

You also have to install the postgres driver locally. On macOS you can use brew.

Depending on your PHP version (try php -v in terminal), use below command:

brew install php71-pdo-pgsql
brew install php70-pdo-pgsql
brew install php55-pdo-pgsql

Also, if you are runninng Valet, make sure to do valet restart afterwards.