1

The latest release of Docker doesn't use a virtual machine anymore, instead using a hypervisor to connect to the containers. This means I can no longer login to postgres with psql:

➜ postgres git:(master) ✗ docker run -d -p 5433:5432 db postgres
<sha>
➜ postgres git:(master) ✗ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
111f3bed4c52 db "/docker-entrypoint.s" 17 minutes ago Up 17 minutes 0.0.0.0:5433->5432/tcp zen_hugle
➜ postgres git:(master) ✗ psql -p 5433 -U postgres
psql: could not connect to server: No such file or directory
 Is the server running locally and accepting
 connections on Unix domain socket "/tmp/.s.PGSQL.5433"?

I have also tried specifying localhost as the host, but that results in a strange output:

➜ postgres git:(master) ✗ psql -h localhost -p 5433 -U postgres
psql: %

Does anyone know what to do in this case? Thank you.

asked Aug 7, 2016 at 20:11
3
  • 1
    That second case looks like you are connected. Is something not working with that? Commented Aug 9, 2016 at 12:54
  • 1
    I'm not sure it's the problem of Docker. Can you connect if you add -h localhost to the psql command? Commented Aug 9, 2016 at 13:40
  • Sorry about the delay in responding. Yes, I can connect to the local postgres database at the port 5432. But I cannot connect to the one at 5433, which is in the docker container. Commented Aug 14, 2016 at 0:01

1 Answer 1

1

I had the same problem today. I discovered that you do have to specify the -h flag for it to work:

psql -U postgres -p 5433 -h localhost

You can also use the URI style connection string:

psql postgresql://postgres:<your_password>@localhost:5433

The default is "local socket" which does not work in the case of docker.

answered Jan 21, 2020 at 21:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.