132

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 going on here and how I should correct it.

6
  • 2
    Have you checked that the server is started? What os are you using?
    – some
    Commented Dec 29, 2013 at 16:34
  • 2
    Did you do what the error message says? "Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections" . You have not shown what is producing that error message. Commented Dec 29, 2013 at 22:35
  • 1
    Also, always show your PostgreSQL version, how you installed PostgreSQL, and your OS/distro/version. Commented Dec 29, 2013 at 22:43
  • 1
    Launch/Inicia con el $>docker-compose up Commented Nov 9, 2022 at 19:01
  • In my case I had to restart my posgres via: sudo service postgresql restart Commented Aug 31, 2023 at 7:59

1 Answer 1

181

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?
13
  • 2
    I tried these commands but nothing happend
    – Rose18
    Commented Dec 30, 2013 at 16:01
  • 13
    I just realized I was connecting to a wrong port 5432 instead of 5433. Check here cat /etc/postgresql/9.6/main/postgresql.conf | grep port. Apartamenty default port changed from version to version. Commented Jul 4, 2017 at 14:55
  • 2
    @JanuszSkonieczny That's only on Debian/Ubuntu with pg_wrapper managed installations. See pg_lscluster. Commented Jul 5, 2017 at 1:36
  • 15
    I think the answer should talk about difference of 127.0.0.1 vs 0.0.0.0 vs localhost and if docker is involved or not between prisma (usually run insider docker container) vs postgres in docker or on native OS.
    – bjm88
    Commented Jul 7, 2018 at 4:31
  • 4
    uncommenting and adding listen_addresses helped me. Thanks Commented Aug 29, 2019 at 8:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.