2

i have been trying out docker container for postgres. So far i have not been able to connect to the database inside the container.

My steps to recreate the problem below.

dockerfile start

FROM postgres
ENV POSTGRES_DB db 
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
COPY db_schema.sql /docker-entrypoint-initdb.d/

I built the docker file like so

$ docker build -t db_con .

And created a container

$ docker run -it -p 5432:5432  --name test_db db_con /bin/bash

View of running container as below

$ docker ps -a

CONTAINER ID    IMAGE   COMMAND                  CREATED       STATUS       PORTS                    NAMES
82347f1114c4    db   "docker-entrypoint..."      3 hours ago    Up 2 sec    0.0.0.0:5432->5432/tcp   test_db

I inspected the container for the address info..

$ docker inspect test_db

--extract start--
"Networks": {
                "bridge": {
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",

                }
            }
--extract end--

Now, i have tried within the container, but i have NOT been successful. I have tried all the commands below with the error below.

root@82347f1114c4:/# psql -U postgres -h 0.0.0.0 -p 5432 -d db
root@82347f1114c4:/# psql -U postgres -h 172.17.0.1 -p 5432 -d db
root@82347f1114c4:/# psql -U postgres -h 172.17.0.2 -p 5432 -d db
**response for all the above**
psql: could not connect to server: Connection refused
    Is the server running on host "0.0.0.0" and accepting
    TCP/IP connections on port 5432?

I will be delighted if anyone can point me in the right direction. I've hit a wall here, any assistance is much appreciated.

1
  • Try all these stuffs such as creating the schema, connecting to the database instance externally (i.e. outside of the container lifecycle). This is more useful approach. Commented Sep 5, 2017 at 17:56

3 Answers 3

2

it looks like you override default postgres cmd to /bin/bash. Why do you put /bin/bash at the end of command?

docker run -it -p 5432:5432  --name test_db db_con /bin/bash

Try to execute

docker run -it -p 5432:5432  --name test_db db_con

Also, postgres will be available only when db dump was restored.

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

1 Comment

Answer right on point, everything working perfectly using your recommendation.
0

You need to add a new rule in your pg_hba.conf:

nano /etc/postgresql/9.3/main/pg_hba.conf

Add:

host    all             all             [Docker Server IP]/16            md5

Next, you need to uncomment the follow line in the postgres.conf:

listen_addresses = '*'                  # what IP address(es) to listen on;

Now restart your postgres service, and try again.

Comments

0

on a separate window / linux term command line:

  • docker exec -it <DOCKER_IMAGE_NAME> bash (lands you in Docker shell prompt)
  • psql -U postgres

should give a postgres=# command prompt

  • postgres=#\l (Lists Databases)
  • postgres=#\c my_db (connects to my_db database in postgres, akin to USE my_db in MySQL)
  • postgres=#\q (quits postgres prompt back to docker shell)

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.