PHP cannot connect to PostgreSQL on CentOS 7

I have CentOS 7 running in VirtualBox on OSX. All Apache, PHP 5.4 and PostgreSQL 9.2 work. But, when my (simple) php script tries to connect to PostgreSQL, it does not work:

Warning: pg_connect (): Unable to connect to the PostgreSQL server: it may not connect to the server: permission is denied The server is running on host "127.0.0.1" and accepting TCP / IP connections on port 5432? in /var/www/html/pg.php on line 7

Verification:

  • Apache is working
  • PHP is fine, phpinfo () tells me PostgreSQL functions are available
  • PostgreSQL 9.2 works
  • psql can connect to the database using localhost or 127.0.0.1, as well as 192.168.178.111
  • pgAdmin on my Mac can connect to this database using ip address 192.168.178.111
  • iptables is disabled.
  • pg_hba.conf has been changed to accept all connections without a password (stupid, I know):

host all all 0.0.0.0/0 trust

But php cannot 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'); ?> 

The work of internal connections (psql), external connections also work (pgAdmin on my laptop), but the php connection does not work ...

What is going wrong? What am I missing?

+6
source share
1 answer

SELinux probably blocks the database connection.

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

 sudo setsebool -P httpd_can_network_connect_db 1 
+6
source

Source: https://habr.com/ru/post/980446/


All Articles