How do I enable php to work with postgresql?

231,931

Solution 1

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());

Solution 2

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.

Solution 3

For debian/ubuntu install

sudo apt-get install php-pgsql

Solution 4

  • 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

Solution 5

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

LoadFile "C:/Program Files (x86)/PostgreSQL/8.3/bin/libpq.dll"
Share:
231,931
Aaron
Author by

Aaron

Updated on September 25, 2021

Comments

  • Aaron
    Aaron over 2 years
    <?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"

  • Aaron
    Aaron about 12 years
    Hi do you know how I would install it in windows? Sorry I don't have ubuntu yet..
  • Aaron
    Aaron about 12 years
    How do I install that package on windows?
  • rinat.io
    rinat.io about 12 years
    Maybe pecl install pdo_pgsql. Not sure
  • Jorge Olivares
    Jorge Olivares about 12 years
    Depends. You are using wamp or something like that?
  • Jorge Olivares
    Jorge Olivares about 12 years
    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.
  • Maks3w
    Maks3w almost 10 years
    And reload the PHP config (reload apache/nginx etc) after install new extensions
  • IIllIIll
    IIllIIll about 9 years
    I'm still seeing PHP Fatal error: Call to undefined function pg_connect(), although I know I've uncommented it. Ideas?
  • wmfrancia
    wmfrancia about 7 years
    Just an update on this old thread for php7 the command is: sudo apt install php7.0-pgsql
  • Matthew Lock
    Matthew Lock over 6 years
    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
  • Daniel L. VanDenBosch
    Daniel L. VanDenBosch about 6 years
    Where would one find httpd.conf
  • Daniel L. VanDenBosch
    Daniel L. VanDenBosch about 6 years
    Awesome thanks, if I were using xammp would I put this in the C:\xampp folder?
  • Floggedhorse
    Floggedhorse about 5 years
    Running php 7.0 - I had to add "extension=pgsql.so" to "php.ini" = "open database successfully" = :)
  • Rishu Ranjan
    Rishu Ranjan almost 3 years
    inside config file for your apache server. If you are using mamp, then C:\MAMP\bin\apache\conf\httpd.conf
  • Eduardo Sebastian
    Eduardo Sebastian over 2 years
    Generally, the command is sudo apt-get install phpVersion-pgsql