Dial tcp 127.0.0.1:5432: connect: connection refused

I am trying to connect Postrgre with following setting:

  1. localhost:5432
  2. Database: database_1
  3. User: postgres
  4. pwd:
  5. SSL Mode: disable
    and getting following error:
    dial tcp 127.0.0.1:5432: connect: connection refused

Can someone please help.

Thanks

Well, do you have a Postgres server running on localhost and listening on port 5432?

You can verify the connectivity using another Postgres client and go back to trying to connect from Grafana once you’ve confirmed that. Maybe you’ve already taken such other troubleshooting steps, but none were mentioned in your question.

  • Have the same problem as mentioned :frowning_face:.
  • I can connect to Postgres through the command line (psql, port 5432, ssl disabled).
  • Grafana simply refuses to recognise the data source.

[Solved]

It appears that I had to specify the docker host IP address instead.

ip add

3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN > group default
link/ether 02:42:e1:c9:03:b7 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global docker0

I then specified the 172.18.0.1 IP address and all was fine :slight_smile:

References:

1 Like

If your container is running on Windows or MacOS then you should also be able to access the docker host’s network at the domain name host.docker.internal.

4 Likes

Thank you!
Your comment also helped me connecting to db from golang.

const ( // DB connection info
host = “host.docker.internal”
port = 5432
user = “postgres”
password = “postgresPass”
dbname = “postgres”
)

3 Likes

Thanks, Docker IP helped me :slight_smile:

same problem, Sir. I do not use docker. Should I really need to use docker to get rid of that error?

No, the problem was caused by the docker install, the solution was how to fix a docker problem.
Tell us more about your setup.

Hello.
Had the same problem on Win10. putting host.docker.internal as host solved my problem.
Followed instruction below:

Same problem here…

  • Don’t use docker
  • PostgreSQL runs local on my machine
  • tried localhost:5432 and 127.0.0.1:5432
    error: dial tcp 127.0.0.1:5432: connect: connection refused

has anyone found another solution yet??

thanks!

as @rostislaved said. I was trynig to connect to postgres from golang and this is what solved it.
Thank you @fcnz !

Curious to know if there’s a solution. Brand new install, trying to connect a local Postgresql as data source, getting same error. I’ve tried most things related to pg_hba.conf and postgresql.conf I’ve found online and no dice.

Hi, I am facing a similar issue. The setup is as follows:

  • Grafana running as a pod in kubernetes
  • Postgresql running on centos8 VM in a different network
  • Firewall allows traffic to postgresql VM IP on port 5432 from all the kubernetes host network.
  • Ran a test psql pod and tested connectivity, works fine.
  • When testing from grafana UI with same settings, doesn’t work.

Any clue would be great.

I have this issue for so long. After a long I figured it out finally.
The problem is we are connecting database with our local dsn(database) address. But actually the port running on dokcer is in different address.
type in terminal after running the contaier docker container inspect <postgres container name>
Here go to the network section and find the “IP Address”
Copy this IP address and replace the “localhost” with it. It will work.

Thank you so much this really saved me