Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections [duplicate]

Tags:

postgresql

I am trying to connect postgresql but I am getting this error.

org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. 

My pg_hba.conf file is like this.

 TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD  # IPv4 local connections: host    all             all             127.0.0.1/32            md5 # IPv6 local connections: host    all             all             ::1/128                 md5 

I would be much obliged if anyone please be so kind enough to explain whats hoing on here and how should I correct it.

like image 777
Rose18 Avatar asked Dec 29 '13 13:12

Rose18


People also ask

How do you check that the hostname and port are correct and that the postmaster is accepting TCP IP connections?

check those: ps -f -u postgres should list postgres processes. sudo lsof -n -u postgres |grep LISTEN or sudo netstat -ltnp | grep postgres should show the TCP/IP addresses and ports PostgreSQL is listening on.

Can't connect to server connection refused postgres?

“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.

How do I enable port 5432?

As an alternative you can go to Control Panel -> Systems and Security -> Windows Firewall -> Allow a program or feature through Windows Firewall -> Advanced Settings -> New Rule: Rule Type: Port. TCP or UDP: TCP. Specific local ports: 5432.


1 Answers

The error you quote has nothing to do with pg_hba.conf; it's failing to connect, not failing to authorize the connection.

Do what the error message says:

Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

You haven't shown the command that produces the error. Assuming you're connecting on localhost port 5432 (the defaults for a standard PostgreSQL install), then either:

  • PostgreSQL isn't running

  • PostgreSQL isn't listening for TCP/IP connections (listen_addresses in postgresql.conf)

  • PostgreSQL is only listening on IPv4 (0.0.0.0 or 127.0.0.1) and you're connecting on IPv6 (::1) or vice versa. This seems to be an issue on some older Mac OS X versions that have weird IPv6 socket behaviour, and on some older Windows versions.

  • PostgreSQL is listening on a different port to the one you're connecting on

  • (unlikely) there's an iptables rule blocking loopback connections

(If you are not connecting on localhost, it may also be a network firewall that's blocking TCP/IP connections, but I'm guessing you're using the defaults since you didn't say).

So ... check those:

  • ps -f -u postgres should list postgres processes

  • sudo lsof -n -u postgres |grep LISTEN or sudo netstat -ltnp | grep postgres should show the TCP/IP addresses and ports PostgreSQL is listening on

BTW, I think you must be on an old version. On my 9.3 install, the error is rather more detailed:

$ psql -h localhost -p 12345 psql: could not connect to server: Connection refused         Is the server running on host "localhost" (::1) and accepting         TCP/IP connections on port 12345? 
like image 189
Craig Ringer Avatar answered Sep 28 '22 20:09

Craig Ringer