I run the following to start a postgres server container:
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
What I was expecting was to use dbeaver to connect via localhost:5432 with postgres and mysecretpassword, but I keep getting the following error in dbeaver, indicating my credentials are wrong:
Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused
Connection refused
Am I pointed at the right IP / port? -p doesn’t help and neither does –add-host with host.docker.internal
2
Answers
Very simple fix for me, just replace localhost with:
localhost = 172.17.0.1, so no wonder that didn't work.
How I solved it:
I ran the following command to inspect the container details. Under Networks:bridge:IPAddress: I found the 172.17.0.2 address.
If you just run
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
, the container is started but no ports are exposed. Add the parameter-p 5432:5432
to forward your local port 5432 to port 5432 of the docker container:docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
Now you can connect to postgres on
localhost:5432
. Using an ip address fromdocker inspect
is not recommended because this ip address might change, for example when the container is recreated.