7

I use docker-compose and I try to connect to the postgres database from the web container. I use this URI:

postgresql://hola:hola@postgres/holadb

I get this error:

Connection refused
    Is the server running on host "postgres" (172.18.0.2) and accepting
    TCP/IP connections on port 5432?

docker-compose.yml

version: '2'

services:
    web:
        restart: always
        build: ./web
        expose:
            - "8000"
        volumes:
            - /usr/src/app/project/static
        command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
        depends_on:
            - postgres

    postgres:
        image: postgres:9.6
        ports:
            - "5432:5432"
        environment:
            - POSTGRES_USER=hola
            - POSTGRES_PASSWORD=hola
            - POSTGRES_DB=holadb
        volumes:
            - ./data/postgres:/var/lib/postgresql/data

I remove ./data/postgres before building and running.

Logs

postgres_1  | The files belonging to this database system will be owned by user "postgres".
postgres_1  | This user must also own the server process.
postgres_1  |
postgres_1  | The database cluster will be initialized with locale "en_US.utf8".
postgres_1  | The default database encoding has accordingly been set to "UTF8".
postgres_1  | The default text search configuration will be set to "english".
postgres_1  |
postgres_1  | Data page checksums are disabled.
postgres_1  |
postgres_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1  | creating subdirectories ... ok
postgres_1  | selecting default max_connections ... 100
postgres_1  | selecting default shared_buffers ... 128MB
postgres_1  | selecting dynamic shared memory implementation ... posix
postgres_1  | creating configuration files ... ok
postgres_1  | running bootstrap script ... ok
web_1       | [2017-06-03 16:54:14 +0000] [1] [INFO] Starting gunicorn 19.7.1
web_1       | [2017-06-03 16:54:14 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web_1       | [2017-06-03 16:54:14 +0000] [1] [INFO] Using worker: sync
web_1       | [2017-06-03 16:54:14 +0000] [7] [INFO] Booting worker with pid: 7
web_1       | [2017-06-03 16:54:14 +0000] [8] [INFO] Booting worker with pid: 8
postgres_1  | performing post-bootstrap initialization ... ok
postgres_1  |
postgres_1  | WARNING: enabling "trust" authentication for local connections
postgres_1  | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1  | --auth-local and --auth-host, the next time you run initdb.
postgres_1  | syncing data to disk ... ok
postgres_1  |
postgres_1  | Success. You can now start the database server using:
postgres_1  |
postgres_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1  |
postgres_1  | waiting for server to start....LOG:  database system was shut down at 2017-06-03 16:54:16 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  autovacuum launcher started
postgres_1  |  done
postgres_1  | server started
postgres_1  | CREATE DATABASE
postgres_1  |
postgres_1  | CREATE ROLE
postgres_1  |
postgres_1  |
postgres_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1  |
postgres_1  | LOG:  received fast shutdown request
postgres_1  | LOG:  aborting any active transactions
postgres_1  | LOG:  autovacuum launcher shutting down
postgres_1  | LOG:  shutting down
postgres_1  | waiting for server to shut down....LOG:  database system is shut down
postgres_1  |  done
postgres_1  | server stopped
postgres_1  |
postgres_1  | PostgreSQL init process complete; ready for start up.
postgres_1  |
postgres_1  | LOG:  database system was shut down at 2017-06-03 16:54:18 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  autovacuum launcher started

I don't understand why it does not work. Thank you in advance for your help.

2
  • It seems that the database is not running. The last lines of the logs indicates that it received a shut down and it did not get started again. Also, even if it is started you need to make sure that the database configuration is allowing external connections, it will be the pg_hba.conf file. See it here: postgresql.org/docs/9.1/static/auth-pg-hba-conf.html Commented Jun 1, 2017 at 20:17
  • @JorgeCampos Then, it is written "database system is ready to accept connections." And I read something about that: "The starting and stopping of the server is normal. This allows the entrypoint script to: start the sql server listening only on the unix socket; create initial users, databases, and passwords; and run shell scripts and import sql files from /docker-entrypoint-initdb.d/. After those are finished, it stops the backgrounded sql server, and then replaces itself with a new sql server process listening on the tcp socket." here Commented Jun 1, 2017 at 20:31

2 Answers 2

10

The web container tries to connect while postgres is still initializing... Waiting some delay solved my issue.

EDIT: I use Docker Compose Healthcheck to do this.

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

Comments

7

You need to setup a network to allow communication between containers. Something like this should work:

version: '2'
services:
    web:
        restart: always
        build: ./web
        expose:
            - "8000"
        volumes:
            - /usr/src/app/project/static
        command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
        depends_on:
            - postgres
        networks: ['mynetwork']

    postgres:
        restart: always
        build:
            context: ./postgresql
        volumes_from:
            - data
        ports:
            - "5432:5432"
        networks: ['mynetwork']
networks: {mynetwork: {}}

More info here and here.

7 Comments

Thank you for your answer. A default network is automatically created by docker-compose. In my case: 8ffcf2334734 hola2_default bridge local so it should work without mynetwork. I still tried your solution but it did not work.
You're right. In this case I'd suggest you to connect in your postgres container through sudo docker exec -it postgres bash and then ensure your server is running: psql -U role. Also check if postgres's listen_addresses is set to "*" by issuing SHOW listen_addresses.
Indeed, hola role is not created. I don't understand why. I simplified my architecture and updated my question.
Actually, the role is created (the right command is psql -d holadb -U hola) but still the same error...
You're still getting Connection refused?
|

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.