我在 OSX 上的 VirtualBox 中运行 CentOS 7。 Apache、PHP 5.4 和 PostgreSQL 9.2 都在运行。但是,当我的(简单的)php 脚本尝试连接到 PostgreSQL 时,它不起作用:
Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432? in /var/www/html/pg.php on line 7
检查:
- Apache 正在运行
- PHP 很好,phpinfo() 告诉我 PostgreSQL 函数可用
- PostgreSQL 9.2 正在运行
- psql 可以使用 localhost 或 127.0.0.1 以及 192.168.178.111 连接到数据库
- 我的 Mac 上的 pgAdmin 可以使用 IP 地址 192.168.178.111 连接到这个数据库
- iptables 已关闭
- pg_hba.conf 已更改为无需任何密码即可接受所有连接(愚蠢,我知道):
host all all 0.0.0.0/0 trust
但是php无法连接.... 这是我的脚本:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
echo 'hello world!';
$conn = pg_connect('host=127.0.0.1 port=5432 user=postgres dbname=postgres');
?>
内部连接工作(psql),外部连接也工作(我的笔记本电脑上的 pgAdmin),但是 php 连接不工作...
出了什么问题?我缺少什么?
最佳答案
可能 SELinux 阻止了您的数据库连接。
确保您设置了正确的 bool 值以允许您的 Web 应用程序与数据库对话:
sudo setsebool -P httpd_can_network_connect_db 1
关于centos - PHP 无法连接到 CentOS 7 上的 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749691/