Warning: pg_connect(): Unable to connect to PostgreSQL server:

Posted January 4, 2017 31.6k views
PostgreSQLUbuntu 16.04

i ve postgreSQL in my droplet ( ip= 139.59.xx.xx )
i can connect it with pgadminIII and valentina studio without problem.

but im running php script on my hosting server and im getting some error

Warning: pgconnect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out Is the server running on host “139.59.xx.xx” and accepting TCP/IP connections on port 5432? in /home/f*****/publichtml/ on line 2
Error : Unable to open database

here is my php script which is running @ hosting server to connecto my droplet pgsql( ip= 139.59.xx.xx )

$db = pg_connect(“host=139.59.xx.xx port=5432 dbname=xxxxxxx user=xxxxxx password=xxxxxxxx”);
echo “Error : Unable to open database\n”;
} else {
echo “Opened database successfully\n”;

what should i do about it ?


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer


Run the following command via SSH (on your Droplet):

sudo ufw status

… to see if ufw is enabled (your firewall, which is standard on Ubuntu 16.x). If it is, you will need to allow the connection through the firewall.

To do this, you’d need to get the Dedicated or Shared IP address from and then run:

sudo ufw allow from NAME_IP to any port 5432

… replace NAME_IP with the IP for your account.

  • @jtittle
    Status: inactive

    Should i enable it ?

    • @canf113378ba95d

      If it’s not enabled, connections shouldn’t be blocked, so the next option would be to get in touch with’s support team and see if they are blocking outgoing connections to any specific ports (i.e. 5432). In the support ticket, ask them if they are blocking specific outgoing ports and if you’re allowed to connect to external servers (not all shared hosting providers will allow it).

      If you can connect to your Droplet using the applications noted in your first post, the issue most likely will be related to their servers.

      That being said, in regards to the firewall, for security, yes, it should be enabled. Ideally, you want to enable ufw and set it up with a deny all policy, then add rules that allow only the ports you specify to be accessed.

      The most simple way of doing this would be (copy and paste to terminal):

      sudo ufw default deny \
      && sudo ufw allow 22/tcp \
      && sudo ufw allow 5432/tcp \
      && sudo ufw enable

      What this does is set the default policy to deny all connections, then make an exception to allow port 22 (SSH) and port 5432 (PQSql), and finally enable the firewall. This configuration will deny access to all other ports (80, 443, etc), so if you want to extend the ports that are open for connection, you would simply add a new rule. For instance, port 80 is HTTP, so to add it you’d run:

      sudo ufw allow 80/tcp

      and port 80 can now be accessed.