0

I have recently attempted to install a postgresql database on a digital ocean droplet running Ubuntu 18.04 x64

Using PgAdmin on my local PC, I can connect fine to the database. Using my own C# tool on my local PC, I can connect fine to the database.

Using PHP on another server however, I cannot connect and upon checking the error log I have receiving this -

[19-Jan-2019 16:32:55 UTC] PHP Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused Is the server running on host "178.62.42.168" and accepting TCP/IP connections on port 5432?

My PHP code is as follows, credentials adjusted for posting here but are correct.

<?php
    $db = pg_connect("host=hostip port=5432 user=sam password=sensoredpassword dbname=sam ") or die('connection failed');
?>

Port 5432 is open.

I have adjusted /etc/postgresql/10/main/postgresql.conf >>> "listen_addresses = '*' and uncommented.

I have adjusted /etc/postgresql/10/main/pg_hba.conf and added >>> host all all 0.0.0.0/0 md5

Why can I not connect with PHP but I can connect with other tools and languages? Could something on the server the PHP is running on be blocking the connection?

  • the php is on the same machine where PgAdmin is running? if yes, you might need to whitelist `php` executable on your firewall. though *Connection refused Is the server running on host* seems that you test on different places. like against local server when using the PgAdmin. As connection refused signify that the port is not open on the remote server. – Bagus Tesa Jan 20 '19 at 12:11
  • @BagusTesa The PHP is running on a different machine to where PgAdmin is running. PgAdmin is also running on a different machine to where the database is hosted. The port is most certainly open. https://gyazo.com/ec8efaad187518f9f3b9b0ae2f3531ff – Samuel Clayton Jan 20 '19 at 12:49
  • @SamualClayton, is the firewall open? You might only whitelisted the connection that originates from the machine where PgAdmin installed. please double check the iptables. – Bagus Tesa Jan 20 '19 at 12:56
  • @BagusTesa My firewall is open yes. https://gyazo.com/5803232dfacbe6decbc8aa60afa418b7 – Samuel Clayton Jan 20 '19 at 13:23
  • hmm, network wise looks fine. are you perhaps had `selinux` enabled? its similar with [this case](https://stackoverflow.com/a/21223404/4648586). and be sure to check Digital Ocean's configuration in case it had some additional firewals in front. – Bagus Tesa Jan 20 '19 at 14:30
  • Yes, I've checked that too. selinux is disabled so it's certainly not that. Could there be something else I need to install to allow PHP support? – Samuel Clayton Jan 20 '19 at 14:47
  • i dont think so.. as long as php had `mod_pgsql` enabled it should be able to communicate to `postgresql`, and the error will be different if you dont have the particular module. – Bagus Tesa Jan 20 '19 at 22:58

0 Answers0