Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP can't connect to PostgreSQL on CentOS 7

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:

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

Checks:

  • 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

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

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?

like image 222
Frank Heikens Avatar asked Jan 02 '15 22:01

Frank Heikens


1 Answers

Probably SELinux is blocking your database connection.

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
like image 200
Michael Hampton Avatar answered Sep 24 '22 22:09

Michael Hampton