Enable PHP support for postgresql in Ubuntu 11.04 server
Asked Answered
P

3

21

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.

Punjab answered 3/10, 2011 at 21:44 Comment(1)
echo phpinfo(); and see if the PostgreSQL database is enabledChairmanship
G
51

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.

Gelsemium answered 30/10, 2012 at 9:55 Comment(2)
in Fedora, you can enter dnf install php-pgsqlProvisional
Worked for Ubuntu 14.04, apache2, php5, postgres 9.11.3. Seems weird but I had done: "service apache2 restart" before and skipped the "sudo /etc/init.d/apache2 restart" method. Just now I did the "sudo /etc/init.d/apache2 restart" method instead of using "service" and suddenly I no longer get the PHP/postgress configuration database connection errors any more. Thanks!Rightful
U
4

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> 
Underage answered 16/2, 2017 at 15:40 Comment(0)
P
0

The only conclusion I can come up with is that phppgadmin installed all of the needed packages to make PHP5 connect to PostgreSQL. I have looked at the dependencies, and I believe that I either didn't install them at all or didn't install them correctly.

I require no more help in this arena, as I have a working setup and know at least one method of getting to that point.

Punjab answered 17/10, 2011 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.