PHP无法在CentOS 7上连接PostgreSQL

时间:2022-07-19 22:47:32

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:

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

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

Checks:

检查:

  • Apache is running
  • Apache运行
  • PHP is fine, phpinfo() tells me PostgreSQL functions are available
  • PHP没问题,phpinfo()告诉我可以使用PostgreSQL函数
  • PostgreSQL 9.2 is running
  • PostgreSQL 9.2运行
  • psql can connect to the database using localhost or 127.0.0.1 and also 192.168.178.111
  • psql可以使用localhost或127.0.0.1和192.168.178.111连接到数据库。
  • pgAdmin on my Mac can connect to this database using ip address 192.168.178.111
  • 我的Mac上的pgAdmin可以使用ip地址192.168.178.111连接到这个数据库
  • iptables has been turned off
  • iptables已经关闭。
  • pg_hba.conf has been changed to accept all connections without any password (stupid, I know):
  • pg_hba。conf已被更改为接受所有连接,没有任何密码(我知道这很愚蠢):

host all all 0.0.0.0/0 trust

托管所有0.0.0.0/0信任

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

但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');
?>

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

内部连接可以工作(psql),外部连接也可以工作(我的笔记本电脑上的pgAdmin),但是php连接不能工作……

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

什么错了吗?我错过了什么?

1 个解决方案

#1


6  

Probably SELinux is blocking your database connection.

可能SELinux正在阻塞数据库连接。

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

确保您设置了正确的布尔值,以允许您的web应用程序与数据库对话:

sudo setsebool -P httpd_can_network_connect_db 1

#1


6  

Probably SELinux is blocking your database connection.

可能SELinux正在阻塞数据库连接。

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

确保您设置了正确的布尔值,以允许您的web应用程序与数据库对话:

sudo setsebool -P httpd_can_network_connect_db 1