I am trying to connect to a Postgres database installed in a remote server using the following command:
psql -h host_ip
-U db_username
-d db_name
This is the error that occurs:
psql: could not connect to server: Connection refused Is the server running on host "<host_ip>" and accepting TCP/IP connections on port 5432?
I already tried the following but the issue remains unresolved:
pg_hba.conf
file to includehost all all 0.0.0.0/0 md5
listen_addresses='*'
nmap
gave me the following output:Starting Nmap 6.47 ( http://nmap.org ) at 2015-09-07 18:08 IST Nmap scan report for 10.17.250.250 Host is up (0.0000040s latency). Not shown: 997 closed ports PORT STATE SERVICE 22/tcp open ssh 25/tcp open smtp 80/tcp open http
Am I missing something? Hope someone can help.
“Could not connect to server: Connection refused” To be sure that PostgreSQL is running, you can also restart it with systemctl restart postgresql. If this does not fix the problem, the most likely cause of this error is that PostgreSQL is not configured to allow TCP/IP connections.
If the connection is still failing, then there might be a permission issue in the pg_hba. conf access configuration file. Make sure this is set up correctly to accept connections from your IP. Finally, ensure there are no firewalls or iptables on the Postgres server that are blocking connections.
cd /etc/postgresql/9.x/main/
open file named postgresql.conf
sudo vi postgresql.conf
add this line to that file
listen_addresses = '*'
then open file named pg_hba.conf
sudo vi pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
sudo /etc/init.d/postgresql restart
Check the port defined in postgresql.conf
. My installation of postgres 9.4 uses port 5433
instead of 5432
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With