0

I'm trying to run a docker container that contains postgres and access its localhost on my application. However, I am getting this error:

UserRDD$ docker run -i --rm -e PGPASSWORD=12qw --network=host postgres:10.9 psql -h localhost -p 5433 -U postgres -d postgres -f -

psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5433?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5433?

The same code seems to be running for someone else. Not sure what I am missing.

Can you please let me know what I am missing. Been trying to struggle with it for the last 3 hours.

Thanks

4
  • How are you running the database? Do you need to run the client in a container, or can you install the PostgreSQL command-line tools and just run psql from the host? Commented Nov 5, 2019 at 17:08
  • @DavidMaze I guess, that he has postgres in docker and the problem get access to container from localhost Commented Nov 5, 2019 at 17:13
  • Attach the result of the docker ps command Commented Nov 5, 2019 at 17:14
  • @MaximKasyanov yes you are right. I'm trying to access postgres inside the docker container. docker ps returns empty because the docker isn't started because of this failure. I did however, just run docker run -i --rm -e PGPASSWORD=12qw --network=host postgres:10.9 without the second argument and that started the docker. I'm so confused. Commented Nov 5, 2019 at 17:20

2 Answers 2

1

Use the following command with port mapping to expose container port to localhost -p <host_port>:<container_port>

docker run  -e POSTGRES_PASSWORD=12qw  -e POSTGRES_DB=stack -p 1111:5432 -d postgres

After that, you could access to this postgres instance on localhost:1111 port

psql -h localhost -p 1111 -U postgres -d stack
Sign up to request clarification or add additional context in comments.

Comments

0

The reason is Postgres DB is not running as you override the default CMD which just start Postgres client (psql), not DB server. for DB server the argument should be postgres.

docker run -i --rm -e PGPASSWORD=12qw --network=host postgres:10.9 postgres

As your command does not start Postgres server, but there is only Postgres client that trying to connect with localhost of the container but the Postgres server is not running. it will make sense like

docker run -i --rm  postgres:10.9 psql -h remote-server -p 5432 -U postgres -d postgres

To run postgres server you only need

docker run  -e POSTGRES_PASSWORD=12qw  -e POSTGRES_DB=test -p 5432:5432 -d postgres:10.9

then connect with db after DB is up.

Another important thing any argument that is passed to run command will be treated as an argument to postgres server, for example

docker run -i --rm  postgres:10.9  --version

Comments

Your Answer

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.