0

i try on my localhost recommended commands to learn playing with docker. The exact command is :

docker run -it --rm postgres psql

The error message i get is :

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

In fact the file .s.PGSQL.5432 does no exist in the container, while it exists on the host machine. So, what is wrong in my reasoning/command ?

1
  • When you run psql like that, it expects a running instance of postgres that it can connect to. Do you have that? Commented Nov 10, 2022 at 14:35

1 Answer 1

1

You should think of containers as, conceptually, separate machines. Separate from the host and separate from each other.

When you run psql without any parameters, like you do here, it'll look for a postgres database running on the local machine, on port 5432. But since psql is running in a container it looks for the database inside the container. And there isn't one. That's what the error message is trying to tell you.

To get it to work, you need to specify the -h parameter on the psql command to tell it where the database is located. To get the address of the host machine, you can add --add-host=host.docker.internal:host-gateway to the docker run command. It's customary to call the host host.docker.internal.

So you end up with the command

docker run -it --rm --add-host=host.docker.internal:host-gateway postgres psql -h host.docker.internal

which should then let you connect to the postgres database on the host machine.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.