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 the error that occurs:
psql: could not connect to server: Connection refused Is the server running on host "" and accepting TCP/IP connections on port 5432?
- Postgres installed version is 9.4.
- Host operating system : Ubuntu 15.04
- Client operating system : Centos 7
I already tried the following but the issue remains unresolved:
- Edited
pg_hba.conf
file to include
host all all 0.0.0.0/0 md5
- Edited 'postgresql.conf' and changed the listen parameter to
listen_addresses='*'
- Restarted postgres service.
- Disabled firewall and iptables on host and client.
- I checked by running the psql command locally and it worked.
- I tried the second solution given in this question. Running
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.
The following helped me on macos Mojave:
Make sure the settings are applied correctly in the config file.
Try the following to see the logs and find your problem.
Check the port defined in
postgresql.conf
. My installation of postgres 9.4 uses port5433
instead of5432
open file named
postgresql.conf
add this line to that file
then open file named
pg_hba.conf
and add this line to that file
It allows access to all databases for all users with an encrypted password
restart your server
I have struggled with this when trying to remotely connect to a new PostgreSQL installation on my Raspberry Pi. Here's the full breakdown of what I did to resolve this issue:
First, open the PostgreSQL configuration file and make sure that the service is going to listen outside of localhost.
I used
nano
, but you can use the editor of your choice, and while I have version9.1
installed, that directory will be for whichever version you have installed.Search down to the section titled 'Connections and Authentication'. The first setting should be
'listen_addresses'
, and might look like this:The comments to the right give good instructions on how to change this field, and using the suggested
'*'
for all will work well.Please note that this field is commented out with #. Per the comments, it will default to 'localhost', so just changing the value to
'*'
isn't enough, you also need to uncomment the setting by removing the leading#
.It should now look like this:
You can also check the next setting, 'port', to make sure that you're connecting correctly. 5432 is the default, and is the port that psql will try to connect to if you don't specify one.
Save and close the file, then open the Client Authentication config file, which is in the same directory:
I recommend reading the file if you want to restrict access, but for basic open connections you'll jump to the bottom of the file and add a line like this:
You can press tab instead of space to line the fields up with the existing columns if you like.
Personally, I instead added a row that looked like this:
This restricts the connection to just the one user and just the one database on the local area network subnet.
Once you've saved changes to the file you will need to restart the service to implement the changes.
Now you can check to make sure that the service is openly listening on the correct port by using the following command:
If you don't run it as elevated (using
sudo
) it doesn't tell you the names of the processes listening on those ports.One of the processes should be Postgres, and the Local Address should be open (0.0.0.0) and not restricted to local traffic only (127.0.0.1). If it isn't open, then you'll need to double check your config files and restart the service. You can again confirm that the service is listening on the correct port (default is
5432
, but your configuration could be different).Finally you'll be able to successfully connect from a remote computer using the command:
In my case, I did not change azure default security policy in management portal. The original is port 22 allowed and the rest are all denied. As long as I add 5432 port, everything becomes good.