Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable PHP support for postgresql in Ubuntu 11.04 server

Tags:

I've installed Apache2 with php5 support and everything works there. I've installed PostgreSQL and am able to connect to it using the terminal and execute SQL statements.

PROBLEM: I can't get a working connection between my php scripts and the PostgreSQL database. I have installed the php5-pgsql packaged from the repositories, but the connection just won't work.

I get the following error message:

PHP Fatal Error: Call to undefined function pg_connect() in /var/www/[myfile].php on line [X]

How do I enable support for PostgreSQL connections in PHP5 in ubuntu 11.04?

EDIT: Checked phpinfo() and found no entries for PostgreSQL. I don't know why that is so, I DID install php5-pgsql package for ubuntu 11.04.

Here is what worked: I installed phppgadmin from the Ubuntu repositories. Not only does this make a nice tool available for me now, it also installed the needed packages for php to connect to postgresql.

After that, it was all in the connection parameters. It wouldn't connect to the database on the local server until I defined the connection host, port, database, user, and password in that order in pg_connect().

I still don't know why installing php5-pgsql on my own didn't enable PostgreSQL connections from php. Any input on this would be helpful.

like image 649
Adam Avatar asked Oct 03 '11 21:10

Adam


People also ask

Which PHP extensions need to be enabled for PostgreSQL?

Which PHP extensions need to be enabled for PostgreSQL? PHP 5.4 or higher is required: 5.4, 5.5, 5.6, 7.0, You need to chose between, PostgeSQL PHP extension and PostgreSQL PDO extension. You need to activate the extension you chose.

How can I connect PostgreSQL database using PHP?

php $db_handle = pg_connect("host=localhost dbname=edb user=enterprisedb password=postgres"); if ($db_handle) { echo 'Connection attempt succeeded. '; } else { echo 'Connection attempt failed. '; } echo "<h3>Connection Information</h3>"; echo "DATABASE NAME:" .

Does PostgreSQL support PHP?

PHP provides many functions for working directly with PostgreSQL databases. To connect to PostgreSQL using native functions, follow these steps: Use the following PHP code to connect to PostgreSQL and select a database.

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

Install the php5-pgsql package solves the problem. (depending on the version ... php4-pgsql for php4)

apt-get install php5-pgsql 

Remember to restart Apache.

/etc/init.d/apache2 restart 

--Note that it might be hard if you do not administer your server.

like image 125
Nicolaj Schweitz Avatar answered Sep 19 '22 06:09

Nicolaj Schweitz


Currently, I am using Ubuntu 16.04 LTS. Me too was facing same problem while Fetching the Postgress Database values using Php so i resolved it by using the below commands.

Mine PHP version is 7.0, so i tried the below command.

apt-get install php-pgsql

Remember to restart Apache.

/etc/init.d/apache2 restart 

Below is my code , might be someone get benefited :

- testdb.php

<html>      <body>          <table border="0" cellspacing="0" cellpadding="0">              <tr>                  <td>                      Friend ID                  </td>                  <td>                       Name                  </td>               </tr>          <?php          $db = pg_connect('host=localhost dbname=postgres user=postgres password=root port=5432');           $query = "SELECT * FROM account"; //account is name of table           $result = pg_query($query);          if (!$result) {              echo "Problem with query " . $query . "<br/>";              echo pg_last_error();              exit();          }           while($myrow = pg_fetch_assoc($result)) {              printf ("<tr><td>%s</td><td>%s</td></tr>", $myrow['id'], htmlspecialchars($myrow['name']));         }          ?>          </table>      </body>  </html>  
like image 34
MD Shahrouq Avatar answered Sep 22 '22 06:09

MD Shahrouq