BlowMan
BlowMan

Reputation: 373

Docker container Postgres connection error

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.

Upvotes: 2

Views: 10097

Answers (3)

Rahul Saini
Rahul Saini

Reputation: 2353

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)

Upvotes: 0

Camilo Camargo
Camilo Camargo

Reputation: 61

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.

Upvotes: 0

Bukharov Sergey
Bukharov Sergey

Reputation: 10185

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.

Upvotes: 2

Related Questions