简体   繁体   English

在Ubuntu 11.04服务器中为Postgresql启用PHP支持

[英]Enable PHP support for postgresql in Ubuntu 11.04 server

I've installed Apache2 with php5 support and everything works there. 我已经安装了支持php5的Apache2,并且一切正常。 I've installed PostgreSQL and am able to connect to it using the terminal and execute SQL statements. 我已经安装了PostgreSQL,并且能够使用终端连接到它并执行SQL语句。

PROBLEM: I can't get a working connection between my php scripts and the PostgreSQL database. 问题:我的php脚本和PostgreSQL数据库之间无法建立有效的连接。 I have installed the php5-pgsql packaged from the repositories, but the connection just won't work. 我已经安装了从存储库打包的php5-pgsql,但是连接不起作用。

I get the following error message: 我收到以下错误消息:

PHP Fatal Error: Call to undefined function pg_connect() in /var/www/[myfile].php on line [X] PHP致命错误:在第[X]行上的/var/www/[myfile].php中调用未定义的函数pg_connect()

How do I enable support for PostgreSQL connections in PHP5 in ubuntu 11.04? 如何在ubuntu 11.04中的PHP5中启用对PostgreSQL连接的支持?

EDIT: Checked phpinfo() and found no entries for PostgreSQL. 编辑:检查phpinfo()并发现PostgreSQL没有条目。 I don't know why that is so, I DID install php5-pgsql package for ubuntu 11.04. 我不知道为什么,我为ubuntu 11.04安装了php5-pgsql软件包。

Here is what worked: I installed phppgadmin from the Ubuntu repositories. 这是起作用的方法:我从Ubuntu存储库中安装了phppgadmin。 Not only does this make a nice tool available for me now, it also installed the needed packages for php to connect to postgresql. 这不仅使我现在有了一个不错的工具,而且还安装了php所需的软件包以连接到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(). 直到我在pg_connect()中按此顺序定义连接主机,端口,数据库,用户和密码后,它才会连接到本地服务器上的数据库。

I still don't know why installing php5-pgsql on my own didn't enable PostgreSQL connections from php. 我仍然不知道为什么我自己安装php5-pgsql不能启用php的PostgreSQL连接。 Any input on this would be helpful. 任何对此的投入将是有帮助的。

Install the php5-pgsql package solves the problem . 安装php5-pgsql软件包即可解决问题 (depending on the version ... php4-pgsql for php4) (取决于版本... php4-pgsql for php4)

apt-get install php5-pgsql

Remember to restart Apache. 请记住重新启动Apache。

/etc/init.d/apache2 restart

--Note that it might be hard if you do not administer your server. -请注意,如果不管理服务器,可能会很难。

Currently, I am using Ubuntu 16.04 LTS. 当前,我正在使用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. 当我使用Php提取Postgress数据库值时,我也面临着同样的问题,所以我使用以下命令解决了它。

Mine PHP version is 7.0, so i tried the below command. 我的PHP版本是7.0,因此我尝试了以下命令。

apt-get install php-pgsql

Remember to restart Apache . 请记住重新启动Apache

/etc/init.d/apache2 restart

Below is my code , might be someone get benefited : 以下是我的代码,可能有人会受益:

- testdb.php -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> 

The only conclusion I can come up with is that phppgadmin installed all of the needed packages to make PHP5 connect to PostgreSQL. 我能得出的唯一结论是phppgadmin安装了所有必需的软件包,以使PHP5连接到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. 在这个领域,我不需要任何更多的帮助,因为我的设置可以正常工作,并且至少知道达到这一点的一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM