62
<?php

try {
   $dbh = new PDO('pgsql:host=localhost;port=5432;dbname=###;user=###;password=##');
   echo "PDO connection object created";
}
catch(PDOException $e)
{
      echo $e->getMessage();
}

?>

I get the error message "Could Not Load Driver"

2

14 Answers 14

93

You need to install the pgsql module for php. In debian/ubuntu is something like this:

sudo apt-get install php5-pgsql

Or if the package is installed, you need to enable de module in php.ini

extension=php_pgsql.dll (windows)
extension=php_pgsql.so (linux)

Greatings.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi do you know how I would install it in windows? Sorry I don't have ubuntu yet..
Depends. You are using wamp or something like that?
try to enable the module first, editing php.ini and searching for php_pgsql and remove the ; in the begining of the line. Then you need to reload your service.
Just an update on this old thread for php7 the command is: sudo apt install php7.0-pgsql
Running php 7.0 - I had to add "extension=pgsql.so" to "php.ini" = "open database successfully" = :)
|
65

Try this:

Uncomment the following in php.ini by removing the ";"

;extension=php_pgsql.dll

Use the following code to connect to a postgresql database server:

pg_connect("host=localhost dbname=dbname user=username password=password")
    or die("Can't connect to database".pg_last_error());

2 Comments

I'm still seeing PHP Fatal error: Call to undefined function pg_connect(), although I know I've uncommented it. Ideas?
Related to Postgres I had the line extension=pdo_pgsql, after uncommenting the error was solved
22

For debian/ubuntu install

sudo apt-get install php-pgsql

1 Comment

This is tagged Windows. Linux suggestions are unhelpful.
8
  • SO: Windows/Linux
  • HTTP Web Server: Apache
  • Programming language: PHP

Enable PHP to work with PostgreSQL in Apache

In Apache I edit the following configuration file: C:\xampp\php.ini

I make sure to have the following lines uncommented:

extension=php_pgsql.dll 
extension=php_pdo_pgsql.dll

Finally restart Apache before attempting a new connection to the database engine.


Also, I leave my code that ensures that the connection is unique:

private static $pdo = null;

public static function provideDataBaseInstance() {
    if (self::$pdo == null) {
        $dsn = "pgsql:host=" . HOST .
               ";port=5432;dbname=" . DATABASE .
               ";user=" . POSTGRESQL_USER .
               ";password=" . POSTGRESQL_PASSWORD;
        try {
            self::$pdo = new PDO($dsn);
        } catch (PDOException $exception) {
            $msg = $exception->getMessage();
            echo $msg . 
                 ". Do not forget to enable in the web server the database 
                  manager for php and in the database instance authorize the 
                  ip of the server instance if they not in the same 
                  instance.";
        }
    }
    return self::$pdo;
}

GL

Comments

6

I have to add in httpd.conf this line (Windows):

LoadFile "C:/Program Files (x86)/PostgreSQL/8.3/bin/libpq.dll"

2 Comments

Where would one find httpd.conf
inside config file for your apache server. If you are using mamp, then C:\MAMP\bin\apache\conf\httpd.conf
3

just install the database driver:

apt-get install php5-pgsql php5-mysql php5-sqlite ... and so on ...

and be happy!

1 Comment

And reload the PHP config (reload apache/nginx etc) after install new extensions
3

I installed PHP on windows IIS using Windows Platform Installer (WPΙ). WPΙ creates a "PHP Manager" tool in "Internet Information Services (IIS) Manager" console. I am configuring PHP using this tool.

in http://php.net/manual/en/pdo.installation.php says:

PDO and all the major drivers ship with PHP as shared extensions, and simply need to be activated by editing the php.ini file: extension=php_pdo.dll

so i activated the extension using PHP Manager and now PDO works fine

PHP manager simple added the following two lines in my php.ini, you can add the lines by hand. Of course you must restart the web server.

[PHP_PDO_PGSQL]
extension=php_pdo_pgsql.dll

1 Comment

I do installed PHP via WPI, and it was enough for me to add extension=php_pgsql.dll to C:\Program Files\PHP\v7.1\php.ini at the end of the [ExtensionList] section
2

Because some answers seem outdated, if you are working with xampp, go to the php folder inside your xampp installation. In it open the php.ini file and uncomment

extension=pdo_pgsql
extension=pdo_sqlite

Remove the ; before these lines.

Comments

1

You need to isntall pdo_pgsql package

2 Comments

How do I install that package on windows?
Maybe pecl install pdo_pgsql. Not sure
1

I and many other shave spent way too long on this - this needs a massive improvement. After spending hours, I finally copied php_pgsql.dll from php's ext directory to Apache24's root directory (wherever you installed it) and finally Apache was able to get the php/pg modules and dlls loaded.

1 Comment

Awesome thanks, if I were using xammp would I put this in the C:\xampp folder?
1
$dbh = new PDO('pgsql:host=localhost;port=5432;dbname=###;user=###;password=##');

For PDO type connection uncomment

extension=php_pdo_pgsql.dll and comment with

;extension=php_pgsql.dll

$dbh = pg_connect("host=localhost dbname=### user=### password=####");

For pgconnect type connection comment

;extension=php_pdo_pgsql.dll and uncomment

extension=php_pgsql.dll

Both the connections should work.

Comments

0

in my case there are 2 php.ini, I had to uncomment extension pdo_pgsql in both php.ini

  1. in php folder
  2. in apache folder

both inside in wamp folder

Comments

0

If you're using AppServ on Windows, then:

To Enable PHP to work with PostgreSQL in Apache

  1. Go to C:\AppServ\php7\php.ini.
  2. Uncomment this line: ;extension=pgsql removing the ;.

Comments

0

Configure correctly extension directory in php.ini:

; Directory in which the loadable extensions (modules) reside.
; https://php.net/extension-dir
;extension_dir = "./"
; On windows:
;extension_dir = "ext"

If you on Windows just remove ";" on last line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.