CentOS 6.5上的PHP无法连接到Postgres DB

时间:2014-05-07 06:09:32

标签: php linux postgresql centos6

我有一台运行在VirtualBox上的CentOS 6.5虚拟机。 在它上面我设置了Apache,PHP,PHP Postgres扩展(php-pgsql)和Postgres数据库。 我修改了pg_hba.conf文件以允许连接:

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident

host all all 192.168.0.0/24 trust
host all all 0.0.0.0/0 md5

这是我的iptables文件:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:8090 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:postgres 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

现在,当我使用客户端pgAdmin3(用户:postgres,密码:postgres)从我的主机(ip:192.168.0.117)连接到Postgres时,我能够做到这一点,一切正常但在我的CentOS上运行PHP VM无法连接到数据库:

$connection_string = "host=localhost port=5432 dbname=rt_prezzario user=postgres password=postgres";
$resource = pg_connect($connection_string);

这是我在运行这两个代码行时触发的警告:

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

有人有建议吗?

1 个答案:

答案 0 :(得分:6)

SELinux可能会阻止连接。当连接被阻止时,您应该能够在/ var / log / messages中找到日志条目。

尝试在SELinux中设置此标志:

setsebool -P httpd_can_network_connect_db on

RedHat有关于此SELinux标志的一些文档:1