De Ripper De Ripper - 10 months ago 50
Apache Configuration Question

PHP - Connection failed to PostgreSQL database in virtual machine

I am trying to connect to a PostgreSQL database in a virtual machine, through a php script, but being unsuccessful.

I execute the php from the host (Windows 8.1) and the postgresql is at the virtual machine (Debian 8.6). I use VirtualBox.

The error output from the browser is:

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out (0x0000274C/10060) Is the server running on host "" and accepting TCP/IP connections on port 5432? in C:\xampp\htdocs\pg_test.php on line 11

with line 11 being


The parameters passed to

$host = "host=";
$port = "port=5432";
$dbname = "dbname=postgres";
$user = "user=user_switch";
$password = "password=switch";
$conn_string = "$host $port $dbname $user $password";

I use
because I checked (with ifconfig) that the guest has the ip

I modified the files
, to include the lines:


host all all trust


listen_addresses = '*'

I added the rule
-A INPUT -p tcp -m cp --dport 5432 -j ACCEPT

I also tried by disabling the firewall on the Windows host.

In some places they say its SELinux blocking the connection, but I don't have it installed in the guest.

I am able to succesfully connect to the database using the user "user_switch" in the guest with the command
psql -d postgres -U user_switch -W -h localhost
, in which it prompts me for the password.

Thanks in advance, please feel free to ask about more specifications I may have forgotten.

Answer Source

Add this line to pg_hba.conf:

host  all  all  trust

and restart PostgreSQL. This allows any connection to 10.0.2.x from any user.