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

标签 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 时,我能够做到这一点并且一切正常但 PHP 在我的 CentOS 上运行虚拟机无法连接到数据库:

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

这是当我运行这两行代码时 PHP 触发的警告:

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?

有人有建议吗?

最佳答案

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

尝试在 SELinux 中设置这个标志:

setsebool -P httpd_can_network_connect_db on

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

关于CentOS 6.5 上的 PHP 无法连接到 Postgres DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23509994/

相关文章:

php - Prestashop Web 服务返回 JSON

php - 查询15分钟内添加的行

linux - Linux malloc()在ARM与x86上的行为是否有所不同?

postgresql - 如何分别执行两个查询?

PostgreSQL:pg_hba.conf 中的 MD5 身份验证给我 fatal error :用户 "postgres"的对等身份验证失败

php - 如何从cgridview 中获取复选框的值?

linux - git 克隆到主目录

linux - 使用 bash 列出脚本中使用的所有变量

java - 字节 [] 的正确 hibernate 注释

php - 从 SQL 数据库中删除特定数量的行