无法使用 PHP pg_connect() 连接到 PostgreSQL

2022-01-13 00:00:00 postgresql centos php

我刚刚意识到这个问题可能更适合 ServerFault.版主不要复制它,请把它移过来?谢谢.

I just realized that this question may be better suited to ServerFault. Instead of copying it, a moderator please move it over? Thanks.

我检查了 php-info,并且 Postgresql 扩展在那里(pg_connect() 不是未定义的).我还可以使用本地主机上的 psql 连接到 postgresql(我已经适当地编辑了我的 pg_hba.conf 文件).这是不起作用的代码:

I've checked php-info, and the Postgresql extension is there (pg_connect() is not undefined). I am also able to connect to postgresql using psql on localhost (I've edited my pg_hba.conf file appropriately). Here is the code that's not working:

<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=mydb user=myuser password=mypass") or die('Could not connect: ' . pg_last_error());
?>

此代码只会导致浏览器中显示无法连接:".

This code simply results in "Could not connect: " being displayed in the browser.

我检查了我的 apache 日志,这是相关的错误消息:

I checked my apache log, and here's the relevant error message:

PHP Warning:  pg_connect() [<a href='function.pg-connect'>function.pg-connect</a>]: 
Unable to connect to PostgreSQL server: could not connect to server: Permission 
denied
	Is the server running on host &quot;localhost&quot; and accepting
	TCP/IP 
connections on port 5432?

如何修复/调试此问题?

How can I fix/debug this?

我在 Centos 5.4 顺便说一句.

I'm on Centos 5.4 btw.

推荐答案

无法连接到服务器:权限被拒绝

could not connect to server: Permission denied

我在 Centos 5.4 顺便说一句.

I'm on Centos 5.4 btw.

检查 /var/log/audit/audit.log.您可能遇到了 SELinux 规则.

Check /var/log/audit/audit.log. Chances are that you're hitting a SELinux rule.

相关文章