4

I have followed the instructions here to install PostgreSQL through Docker. In fact, I have used the following two commands:

Run
    docker run --name postgresql -d sameersbn/postgresql:9.4-2

Login
    sudo docker exec -it postgresql sudo -u postgres psql

Then I do \conninfo and I get:

# You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432"

What I would like to do next (but not sure how) is to connect into that database thought an external Python application that runs on my local machine (as opposed to another docket).

Should I shoot for localhost:5432 or there is something missing?

2 Answers 2

4

You need to export the port for postgresql, and then you can connect to it as if it were running locally.

docker run --name postgresql -d -p 5432:5432 sameersbn/postgresql:9.4-2

If you don't understand the link above, I suggest you read through the getting starting guide.

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

Comments

2

As @Burhan Khalid said, you can expose your container port using:

docker run --name postgresql -d -p 5432:5432 sameersbn/postgresql:9.4-2

To connect to PostgreSQL container, you can direct connect to: 127.0.0.1:5432. You can't use localhost:5432, because it will try to find a socket file in your host (the socket file is in container).

Another solution is use your container IP address. To find the container IP address, you can use:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' <your-container-id>

But, if you restart your container, the container will get a new IP. So, you need to reconfigure your database config.

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.