Centos – Unable to access CentOS 7 Postgresql Server from another CentOS7 using PHP and PDO

centoscentos7PHPpostgresqlselinux

The Beginning

Server 1 is my CentOS 7 production server. It contains Apache, ssh and a Postgresql database.

Server 2 is my CentOS 7 test server. It contains Apache, ssh and a test database.

Both servers are set with firewalld to accept 22, 80 and 443.
Both servers have the following setting.

sudo setsebool httpd_can_network_connect_db 1

How do I know these ports are open?

I can ssh in (port 22). I can see the "Testing 1, 2, 3…" test page via http and https (port 80 and 443). I can access Postgresql by using SQL Maestro, DBeaver, DBSchema, etc. (port 5432).

The Problem

I can access Postgresql on Server 1 using DbSchema, HeidiSQL, Postgresql SQL Maestro, etc, remotely, from anywhere. However, I cannot connect from Server 2 using PHP and PDO. It always leads to [08006] Timeout.

I tried the following, but did not help;
sudo setsebool httpd_can_network_connect 1

The Question

How do I access Postgresql db on Server 1 from Server 2 using PHP? I'm new to CentOS7. I have done this before using other Linux distro with no issues. What SELinux/other settings that I missing?

The Solution (below)

See Alexander's answer below. I thought the 5432 was enabled in firewall-cmd.
When I issued the following command

sudo firewall-cmd --zone=public --list-services

only dhcpv6-client http ssh https were listed! What happened to my postgresql?
I simply issued the following to make the service permanent;

sudo firewall-cmd --add-service=postgresql --permanent

Best Answer

As you mentioned in you question, you have opened only three ports in firewalld. You should open port 5432 on firewall.

Also you should check on which IP listening postgres, it could listening only on 127.0.0.1:5432.