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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3481736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:55:53  来源:igfitidea点击:

Can't Connect to PostgreSQL with PHP pg_connect()

phppostgresqlcentos

提问by ehsanul

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

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

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 psqlon localhost (I've edited my pg_hba.conf file appropriately). Here is the code that's not working:

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

<?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.

此代码只会导致“无法连接:”显示在浏览器中。

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

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

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\n\tIs the server running on host &quot;localhost&quot; and accepting\n\tTCP/IP 
connections on port 5432?

How can I fix/debug this?

我该如何修复/调试这个?

Edit: I'm on Centos 5.4 btw.

编辑:顺便说一句,我在 Centos 5.4 上。

回答by Charles

could not connect to server: Permission denied

Edit: I'm on Centos 5.4 btw.

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

编辑:顺便说一句,我在 Centos 5.4 上。

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

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