PHP 无法在 CentOS 7 上连接到 PostgreSQL [英] PHP can't connect to PostgreSQL on CentOS 7

查看:30
本文介绍了PHP 无法在 CentOS 7 上连接到 PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OSX 上的 VirtualBox 中运行 CentOS 7.Apache、PHP 5.4 和 PostgreSQL 9.2 都在运行.但是,当我的(简单)php 脚本尝试连接到 PostgreSQL 时,它不起作用:

I have CentOS 7 running in VirtualBox on OSX. Apache, PHP 5.4 and PostgreSQL 9.2 are all running. But, when my (simple) php-script tries to connect to PostgreSQL it doesn't work:

警告:pg_connect():无法连接到 PostgreSQL 服务器:可以未连接到服务器:权限被拒绝服务器是否在主机上运行127.0.0.1"并接受端口 5432 上的 TCP/IP 连接?在/var/www/html/pg.php 在第 7 行

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 已更改为接受所有没有任何密码的连接(愚蠢,我知道):

托管所有所有 0.0.0.0/0 信任

host all all 0.0.0.0/0 trust

但是php连接不上....这是我的脚本:

But php can't connect.... This is my script:

<?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连接不起作用......

Internal connections work (psql), external connections also work (pgAdmin on a my laptop), but the php connection doesn't work...

出了什么问题?我错过了什么?

What is going wrong? What is it that I'm missing?

推荐答案

可能 SELinux 阻塞了你的数据库连接.

Probably SELinux is blocking your database connection.

确保您设置了正确的布尔值以允许您的 Web 应用程序与数据库通信:

Make sure that you set the correct boolean to allow your web application to talk to the database:

sudo setsebool -P httpd_can_network_connect_db 1

这篇关于PHP 无法在 CentOS 7 上连接到 PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆