无法通过PHP连接到远程PostgreSQL服务器

时间:2015-01-10 18:51:05

标签: php postgresql php-pgsql

通过远程IP地址连接到PostgreSQL数据库,我通过Windows使用pgAdmin III取得了成功,但每当我尝试使用标准php-pgsql库从本地CentOS 6 Apache Web服务器连接时,我都会收到错误pg_connect()

注意:

  1. 我无法控制远程服务器,但如果需要,可以查询其他信息。
  2. 我的密码确实包含一个特殊字符(虽然它不是引号字符)。
  3. 信用如下:

    Host (IP): xx.xx.xx.xx
    Port: 5432
    DBname: sandbox
    Username: abc
    Password: ***
    MaintenanceDB: template1
    

    这是我试图在本地服务器上运行的PHP代码:

    pg_connect("host=xx.xx.xx.xx port=5432 dbname=sandbox user=abc password=***");
    

    我也尝试过:

    pg_connect("host=xx.xx.xx.xx port=5432 dbname=template1 user=abc password=***");
    

    当我尝试连接时,收到以下错误:

    Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "xx.xx.xx.xx" and accepting TCP/IP connections on port 5432?
    

    我在extension=pgsql.so目录和/etc/php.d/中添加了/取消注释service httpd restart。我甚至打开了我的iptables端口,所以毫无疑问本地防火墙端口被阻止了:

    -A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
    -A INPUT -m state --state NEW -m udp -p udp --dport 5432 -j ACCEPT
    

    提前致谢: - )

1 个答案:

答案 0 :(得分:0)

根据你的postgresql连接问题,这个解决方案可能有效。我只是从php手册中得到这个答案,我不确定,所以请在这里查看更多http://php.net/manual/en/function.pg-connect.php#38291

您应该尝试将host =和port = parts从连接字符串中删除。这听起来很奇怪,但这是Postgre的“选择”。如果您尚未在postgresql.conf中激活TCP / IP端口,则postgresql不接受来自TCP / IP端口的任何传入请求。

相关问题