PHP无法连接到CentOS 7上的PostgreSQL [英] PHP can't connect to PostgreSQL on CentOS 7
问题描述
我有CentOS 7在OSX上的VirtualBox中运行。 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服务器:可能
没有连接到服务器:Permission denied服务器是否在主机
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连接到数据库
- 可以使用ip地址192.168.178.111连接到此数据库
- iptables已关闭
- pg_hba.conf已更改为接受所有连接, password(stupid,I know):
- Apache is running
- PHP is fine, phpinfo() tells me PostgreSQL functions are available
- PostgreSQL 9.2 is running
- psql can connect to the database using localhost or 127.0.0.1 and also 192.168.178.111
- pgAdmin on my Mac can connect to this database using ip address 192.168.178.111
- iptables has been turned off
- pg_hba.conf has been changed to accept all connections without any password (stupid, I know):
host all all 0.0.0.0/0 trust
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...
出了什么问题?
推荐答案
可能是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屋!