Question

I have a CentOS 6.5 virtual machine, which run on VirtualBox. On it I have set up Apache, PHP, PHP Postgres extension (php-pgsql) and Postgres database. I modified the pg_hba.conf file to allow connections:

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident

host all all 192.168.0.0/24 trust
host all all 0.0.0.0/0 md5

This is my iptables file:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:8090 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:postgres 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

Now, when I connect from my host machine (ip: 192.168.0.117) to Postgres with the client pgAdmin3 (user: postgres, password: postgres) I am able to do that and everything works fine but PHP running on my CentOS VM can not connect to database:

$connection_string = "host=localhost port=5432 dbname=rt_prezzario user=postgres password=postgres";
$resource = pg_connect($connection_string);

This is the warning that PHP triggers when I run those two code lines:

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

Anyone have suggestions?

Was it helpful?

Solution

SELinux is likely blocking the connection. You should be able to find a log entry in /var/log/messages when the connection was blocked.

Try setting this flag in SELinux:

setsebool -P httpd_can_network_connect_db on

RedHat has some documentation on this SELinux flag: 1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top