34
votes

pdo is working fine with mysql but with pgsql its giving error 'PDOException' with message 'could not find driver' I've installed php5-pgsql package which also includes pdo_pgsql

http://packages.debian.org/sid/php5-pgsql

This package provides a module for PostgreSQL database connections directly from PHP scripts. It also includes the pdo_pgsql module for use with the PHP Data Object extension.

my dsn is pgsql:dbname=DB;host=192.168.0.2 I am using Ubuntu 10.04

10
Sorry I was checking the wrong server. Its Gentoo and here postgresql is not installed.Dipro Sen
I'd request the moderetors to please delete the question if possibleDipro Sen
on your own questions, you can delete them by yourself by clicking on the appropriate link at the bottom of your questionJMax
I've got this error as well. My issue is I was connecting through a DSN using a format similar to Heroku's: while it says "postgre:", the correct driver for PDO is "pgsql:"igorsantos07
Check my How to Answer (stackoverflow.com/a/53661888/5279996) in another similar publication. GLBraian Coronel

10 Answers

40
votes

This message means you need to install and or activate postgresql extension in PHP

This solution works for me : To install postgresql extension

[sudo] apt-get install php-pgsql

after, for activating it, uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file

enter image description here

Finally, type :

[sudo] /etc/init.d/apache2 restart

To restart apache server if you use apache such as was my case...

22
votes

1) Have you enabled pgsql.so in php.ini (extension=pgsql.so)?

2) Is you Postgresql listenin on 192.168.0.2 interface? (You can check it by netstat -tpln)

3) How you authenticate your access into Postgresql?

8
votes

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.

4
votes

I had the same issue. First of all - check is it enabled in php.ini. Uncomment extension=php_pdo_pgsql...

than set up extension directory!!

extension_dir = "ext" ; for your case it could be other dir.

and do not forget to restart server after changing the config.

4
votes

Try to remove semicolon in front of

extension=pgsql
extension=pgsql.so

included in your php.ini file

You can do that from the XAMPP Control Panel.

enter image description here

2
votes

I had the same problem with another solution. I lost my around 4 hours to solve this problem. Please check the following to solve this problem.

  1. Check php.ini file and remove semicolon from this line

    extension=pgsql

    extension=pdo_pgsql

  2. Restart your apache2 server

    sudo service apache2 restart

  3. Check if your PDO driver has updated in localhost phpinfo()

enter image description here

I did All the things right and still I had this problem. And you know why? Because I had several versions of php installed. So I was running php7.4 in my php cli but localhost was running on php7.2. So always check your php versions.

  1. Check your php version on localhost and terminal cli

enter image description here

1
votes

Here is what I did to solve the problem.

  1. Edit php.ini and remove ; from extension=pdo_pgsql. Also, add extension=pgsql.so to the php.ini file.

  2. Make sure to restart the Apache server before you try to see the result.

0
votes

When you do not have postgresql installed on the same machine that is Apache and PHP; you have to install php-pgsql and don't have to add extensions in php.ini manually in Linux (in Windows yes), because redundancies are generated and this does not work (checked in error.log).

$ sudo apt install php-pgsql

Then you can check the existence of the extension enabled automatically in:

$ sudo nano /etc/php/7.0/apache2/conf.d/10-pdo.ini

Observations: In phpinfo() you will find the directory conf.d/ and the file error.log

GL

0
votes

Just run php --ini and look for Loaded Configuration File in output for the location of php.ini used by your CLI

then check you have enabled the extensions correctly.

-3
votes

Copy libpq.dll from the PHP directory to Apache24\bin (or wherever your installation could be).