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

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: pg_connect(): 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*****/public_html/ 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 )

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

what should i do about it ?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.


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.