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

January 4, 2017 600 views
PostgreSQL Ubuntu 16.04

i ve postgreSQL in my droplet ( ip= 139.59.xx.xx )
i can connect it with pgadminIII and valentina studio without problem.
https://puu.sh/tblvZ/e6c876ed75.png

but im running php script on my Name.com 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/xxxxx.xxxx.net/new.php on line 2
Error : Unable to open database

here is my php script which is running @ Name.com 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 ?

thanks

1 Answer

@canf113378ba95d

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 name.com 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 Name.com'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.

Have another answer? Share your knowledge.