Postgres: Cannot connect to Postgres (9.4) from mac os host and other containers

Created on 3 Sep 2016  路  2Comments  路  Source: docker-library/postgres

Hello,
I just built a fresh image of a postgres. I am on Mac OSX.
My Dockerfile is reduced to the bare minimum :

FROM postgres

EXPOSE 5432

Here is my docker-compose.yml:

version: '2'
services:
  scheduler:
    hostname: scheduler
    build: ./scheduler
    depends_on:
      - db
    links:
      - db
    volumes:
      - ./scheduler:/code

   db:
    hostname: db
    build: ./db
    volumes:
      - ./dump_data:/dump_data
      - ./db:/code
    volumes_from:
     - data
    ports:
      - 5432:5432
    expose:
      - 5432
    depends_on:
      - rabbit
  data:
    image: tianon/true
    volumes:
      - /var/lib/postgresql/data

I have no problem running the container with :

docker-compose run db
Starting db_data_1
LOG:  database system was interrupted; last known up at 2016-09-02 22:17:00 UTC
LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  invalid record length at 14/68F5CCB8
LOG:  redo is not required
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

I can log into this container and execute local psql commands.

But I cannot connect to my database from my host (targetting the docker-machine ip)

psql -h 192.168.99.100  -d records -p 5432 -U postgres
psql: could not connect to server: Connection refused
    Is the server running on host "192.168.99.100" and accepting
    TCP/IP connections on port 5432?

We I log into an other container (scheduler in this case) I can ping the db:

root@scheduler:/code# ping db
PING db (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: icmp_seq=0 ttl=64 time=0.069 ms
64 bytes from 172.20.0.2: icmp_seq=1 ttl=64 time=0.175 ms
...

But I cannot connect to the db container:

root@scheduler:/code#  psql -h db  -d records -p 5432 -U postgres
psql: could not connect to server: Connection refused
    Is the server running on host "db" (172.20.0.3) and accepting
    TCP/IP connections on port 5432?

Do you have any idea of where this issue is coming from ?

Telnet on port 5432 is not working neither from linked container and host :
From container

root@scheduler:/code# telnet db 5432
Trying 172.20.0.3...
telnet: Unable to connect to remote host: Connection refused

From host :

telnet 192.168.99.100 5432
Trying 192.168.99.100...
telnet: connect to address 192.168.99.100: Connection refused
telnet: Unable to connect to remote host

Most helpful comment

Did you provide your own postgresql.conf that is now stored in the data volume? Does it have listen_addresses = '*'?

All 2 comments

Did you provide your own postgresql.conf that is now stored in the data volume? Does it have listen_addresses = '*'?

Thank you very much resolved the problem ! You think that, because I store pg data in a volume, the fresh build of a new postgres image does not override this file that I may have edited in the past? I indeed tried to install postgres with a custom Dockerfile before using the image in the Docker Hub...

Was this page helpful?
0 / 5 - 0 ratings