Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL
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
:
- System Properties -> Advanced tab -> Environment Variables
- In 'System variables' group on lower half of window, scroll through and find the
PATH
entry. - Select it and click Edit
- 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.
- Restart any open command prompts, or to be certain, restart your computer.
This should hopefully resolve any problems. For more information see:
- http://php.net/manual/en/install.pecl.windows.php
- http://webcheatsheet.com/php/install_and_configure.php#extsetup
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.
Hyperion
Updated on February 18, 2021Comments
-
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. Thephp.ini
file was checked in Apache, WAMP (from php folder) and PostgreSQL. Theextension_dir
is correct as it being ->extension_dir = "c:/wamp/bin/php/php5.5.12/ext/"
The
extension=pdo_pgsql.dll
andextension=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
fromC:\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 over 9 yearsThank 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 over 9 yearsThe 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 over 9 yearsYes they do exist in that folder. Don't have the
extension=php_pdo.dll
in theext
nor in thephp.ini
-
lainceline over 9 yearsCan 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 over 9 yearsSorry for the delay. I'll copy/paste the info in the 1st post. Thanks a lot.
-
Hyperion over 9 yearsDo you need the whole thing though or just specific parts of it?
-
lainceline over 9 yearsDon'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 thepgsql
ones showing up there? -
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 over 9 yearsWhat about the line that says which
php.ini
is being loaded? The last thing I can think of is that its loading aphp.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 over 9 yearsLoaded 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 over 9 yearsOk, I set up WampServer and postgres on my Windows machine to test this out. Figured it out - copy
libpq.dll
fromC:\wamp\bin\php\php5.5.12
intoC:\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 over 9 yearsThanks 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 almost 8 yearsAnd if you're using PostgreSQL on a modern Debian-based OS, it's
sudo apt-get -y install php7.0-pgsql
. -
Mugoma J. Okomba over 7 yearsNote 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 almost 7 yearsIf one is using php-fpm they need to restart it, apart from restarting web server (nginx/apache)
-
Ben Wilson over 6 yearsThis was the only thing that worked for me on Ubuntu (tried uncommenting lines in php.ini to no effect)
-
FooBar about 6 years
'default' => 'pgsql'
notpostgres
-
Cesar Romero over 5 yearssolved with this, in my case use
sudo apt-get install php7.1-pgsql
-
Alexandre Martini over 4 yearsdamn you libpq.dll, you get me every time. :D
-
Borjovsky over 4 yearsFinally! 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 over 3 yearsthank you. solved with this, in my case use
sudo apt-get install php7.4-pgsql
-
Hiệp Nguyễn over 2 yearssaved my day~~~~~~