Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable php to work with postgresql?

<?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"

like image 807
Aaron Avatar asked May 17 '12 17:05

Aaron


People also ask

How do I connect to PostgreSQL and PHP?

Connecting to Database php $host = "host = 127.0. 0.1"; $port = "port = 5432"; $dbname = "dbname = testdb"; $credentials = "user = postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!

Does PostgreSQL support PHP?

PostgreSQL Functions (PDO_PGSQL) ¶PDO_PGSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to PostgreSQL databases.

Which function is offered by PHP for connecting to a PostgreSQL?

pg_connect() opens a connection to a PostgreSQL database specified by the connection_string .


2 Answers

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.

like image 122
Jorge Olivares Avatar answered Oct 12 '22 12:10

Jorge Olivares


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()); 
like image 32
Sachin Puri Avatar answered Oct 12 '22 11:10

Sachin Puri