Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL

310,298

Solution 1

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

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

If you are receiving a [PDOException] could not find driver error, check to see 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.

For Windows, the pgsql extensions should come pre-downloaded with the official PHP distribution. Just edit your php.ini and uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so

Also, in php.ini, make sure extension_dir is set to the proper directory. It should be a folder called extensions or ext or similar inside your PHP install directory.

Finally, copy libpq.dll from C:\wamp\bin\php\php5.*\ into C:\wamp\bin\apache*\bin and restart all services through the WampServer interface.

If you still get the exception, you may need to add the postgres \bin directory to your PATH:

  1. System Properties -> Advanced tab -> Environment Variables
  2. In 'System variables' group on lower half of window, scroll through and find the PATH entry.
  3. Select it and click Edit
  4. At the end of the existing entry, put the full path to your postgres bin directory. The bin folder should be located in the root of your postgres installation directory.
  5. Restart any open command prompts, or to be certain, restart your computer.

This should hopefully resolve any problems. For more information see:

Solution 2

For PDOException: could not find driver for MySQL, and if it is Debian based OS,

sudo apt-get -y install php5-mysql

Solution 3

For PHP 7 in Ubuntu you can also do:

sudo apt-get install php7.0-pgsql

So, now you can do not uncomment lines in php.ini

UPD: I have a same error, so problem was not in driver. I changed my database.ini, but every time I saw an error. And I change database config in .env and errors gone.

Solution 4

This worked for me:

$ sudo apt-get install php-gd php-mysql

Solution 5

I realize this is an old question but I found it in a Google search so I'm going to go ahead and answer just in case someone else runs across this. I'm on a Mac and had the same issue, but solved it by using HomeBrew. Once you've got it installed, you can just run this command:

brew install php56-pdo-pgsql

And replace the 56 with whatever version of PHP you're using without the decimal point.

Share:
310,298
Hyperion
Author by

Hyperion

Updated on February 18, 2021

Comments

  • Hyperion
    Hyperion about 3 years

    I'm trying to connect with PostgreSQL database through Laravel in order to do a php artisan migrate but doesn't seem to be directed since it's reading the database name of MySQL.

    Here are the commands from database.php:

    'connections' => array(
    
        'sqlite' => array(
            'driver'   => 'sqlite',
            'database' => __DIR__.'/../database/production.sqlite',
            'prefix'   => '',
        ),
    
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    
        'pgsql' => array(
            'driver'   => 'pgsql',
            'host'     => 'localhost',
            'database' => 'postgres',
            'username' => 'postgres',
            'password' => 'root',
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
        ),
    
        'sqlsrv' => array(
            'driver'   => 'sqlsrv',
            'host'     => 'localhost',
            'database' => 'database',
            'username' => 'root',
            'password' => '',
            'prefix'   => '',
        ),
    
    ),
    

    If I remove the MySQL paths I'll get:

    [InvalidArgumentException]
    Database [mysql] not configured.
    


    EDIT: When trying to do php artisan migrate I get a 'PDOException: could not find driver'. I'm using WAMP and I'm in Win8.1. Using PostgreSQL as database.


    EDIT: Have experimented a series of alternative solutions but I'm still ought to get this solved. The php.ini file was checked in Apache, WAMP (from php folder) and PostgreSQL. The extension_dir is correct as it being -> extension_dir = "c:/wamp/bin/php/php5.5.12/ext/"

    The extension=pdo_pgsql.dll and extension=pgsql.dll are uncommented.

    Done the PATH trick in the 'System Variables' and rebooted. No chance.

    Thanks for the help so far.

    These are my drivers php_pdo_driver.h & php_pdo.h from C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\php\SDK\include\ext\pdo

    Information from phpinfo:

    PHP Version 5.5.12

    Compiler MSVC11 (Visual C++ 2012) Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=C:\php-sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8=C:\php-sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8-11g=C:\php-sdk\oracle\x64\instantclient11\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--disable-static-analyze" "--with-pgo"

  • Hyperion
    Hyperion over 9 years
    Thank you very much for your help. Please check the edit part in my post. BTW, the default database path was my first issue that was taken care already, the PDO driver problem came right after.
  • lainceline
    lainceline over 9 years
    The DLL files exist in the extensions folder right? Also, try uncommenting another DLL: extension=php_pdo.dll That's the main PDO driver that the database specific ones rely on.
  • Hyperion
    Hyperion over 9 years
    Yes they do exist in that folder. Don't have the extension=php_pdo.dll in the ext nor in the php.ini
  • lainceline
    lainceline over 9 years
    Can you make a quick php script with phpinfo();? It'll give you the actual extension directory and php.ini that's being loaded. There might be a conflict going on.
  • Hyperion
    Hyperion over 9 years
    Sorry for the delay. I'll copy/paste the info in the 1st post. Thanks a lot.
  • Hyperion
    Hyperion over 9 years
    Do you need the whole thing though or just specific parts of it?
  • lainceline
    lainceline over 9 years
    Don't paste the whole thing. I'm curious about the extension_dir setting and the loaded configuration file. Also, you should have a list in there of all the extensions that are being loaded; are the pgsql ones showing up there?
  • Hyperion
    Hyperion over 9 years
    extension_dir -> c:/wamp/bin/php/php5.5.12/ext/ It seems there's hardly any reference to pgsql. It stats the only PDO drivers enabled are from mysql and sqlite. Don't see reference to the extensions other than the extension_dir. Thank you.
  • lainceline
    lainceline over 9 years
    What about the line that says which php.ini is being loaded? The last thing I can think of is that its loading a php.ini that doesn't have the extension lines in it. If that ends up being fine you might want to check out Laravel Homestead - it makes it very easy to get a development VirtualBox set up and working.
  • Hyperion
    Hyperion over 9 years
    Loaded Configuration File: C:\wamp\bin\apache\apache2.4.9\bin\php.ini ... checked and it has -> extension_dir = "c:/wamp/bin/php/php5.5.12/ext/"
  • lainceline
    lainceline over 9 years
    Ok, I set up WampServer and postgres on my Windows machine to test this out. Figured it out - copy libpq.dll from C:\wamp\bin\php\php5.5.12 into C:\wamp\bin\apache\apache2.4.9\bin, and restart all services through WampServer. The pgsql extensions will now load, and the driver exception should be resolved.
  • Hyperion
    Hyperion over 9 years
    Thanks for all the work my friend. Really. I'd give you reputation points but it seems I need to have over 15 myself. Your solution didn't work so I did a fresh installation of everything, cleared the registry and now it finally works!!! I thought I was gonna quit PostgreSQL and move to MySQL. Thanks a lot!!!
  • Ben Johnson
    Ben Johnson almost 8 years
    And if you're using PostgreSQL on a modern Debian-based OS, it's sudo apt-get -y install php7.0-pgsql.
  • Mugoma J. Okomba
    Mugoma J. Okomba over 7 years
    Note that, for those using Apache or other web server, one needs to restart Apache for the changes to take effect: sudo systemctl restart httpd.service
  • Mugoma J. Okomba
    Mugoma J. Okomba almost 7 years
    If one is using php-fpm they need to restart it, apart from restarting web server (nginx/apache)
  • Ben Wilson
    Ben Wilson over 6 years
    This was the only thing that worked for me on Ubuntu (tried uncommenting lines in php.ini to no effect)
  • FooBar
    FooBar about 6 years
    'default' => 'pgsql' not postgres
  • Cesar Romero
    Cesar Romero over 5 years
    solved with this, in my case use sudo apt-get install php7.1-pgsql
  • Alexandre Martini
    Alexandre Martini over 4 years
    damn you libpq.dll, you get me every time. :D
  • Borjovsky
    Borjovsky over 4 years
    Finally! I was trying it for 2 hours on Windows 10 Wamp Server, and the problem was that Wamp's options affects only the php.ini file in the apache folder, but not in the php folder. Thanks a lot!
  • Mauro Lacerda
    Mauro Lacerda over 3 years
    thank you. solved with this, in my case use sudo apt-get install php7.4-pgsql
  • Hiệp Nguyễn
    Hiệp Nguyễn over 2 years
    saved my day~~~~~~