centos - PHP 无法连接到 CentOS 7 上的 PostgreSQL

标签 centos php postgresql centos7

我在 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/

相关文章:

centos - 主机重启后立即在容器中循环设备设置(losetup、mount 等)失败

c++ - BOOST_LOG_TRIVIAL ( severity ) << ... 不重定向到文件

php - 将图像存储在 MySQL BLOB 中

PostgreSql: 'utf8' 编解码器无法解码位置 42 中的字节 0xe9:无效的连续字节

php - 找不到 Laravel 5.1 CentOs7 部署在此服务器上找不到请求的 URL

xml - 使用 shell 脚本(centos 环境)从 postgresql artifacoty 获取最新版本的 postgresql 版本

php - 全局 PHP 变量在同一文件内的函数内返回 NULL

javascript - jquery (ajax) click 使 php 脚本运行多次

postgresql,将列转换为没有rowid的标题

postgresql - 勺子转换更新给出 'duplicate key value violates unique constraint'