skip to Main Content

I am using docker-compose.yml to create multiple running containers but failing to start Postgres docker server, with following logs and yes I have searched many related SO posts, but they didn’t helped me out.

Creating network "complex_default" with the default driver
Creating complex_server_1   ... done
Creating complex_redis_1    ... done
Creating complex_postgres_1 ... done
Attaching to complex_postgres_1, complex_redis_1, complex_server_1
postgres_1  | Error: Database is uninitialized and superuser password is not specified.
postgres_1  |        You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres_1  |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres_1  |
postgres_1  |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres_1  |        connections without a password. This is *not* recommended.
postgres_1  |
postgres_1  |        See PostgreSQL documentation about "trust":
postgres_1  |        https://www.postgresql.org/docs/current/auth-trust.html
complex_postgres_1 exited with code 1

below is my docker-compose configuration:

version: '3'
services:
    postgres:
        image: 'postgres:11-alpine'
    redis:
        image: 'redis:latest'
    server:
        build:
            dockerfile: Dockerfile.dev
            context: ./server
        volumes:
            - /app/node_modules
            - ./server:/app
        environment:
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - PGUSER=postgres
            - PGHOST=postgres
            - PGDATABASE=postgres
            - PGPASSWORD=postgres_password
            - PGPORT=5432

as well as package.json inside server directory is following:

{
    "dependencies": {
        "body-parser": "^1.19.0",
        "cors": "^2.8.4",
        "express": "^4.16.3",
        "nodemon": "^2.0.4",
        "pg": "7.4.3",
        "redis": "^2.8.0"
    },
    "scripts": {
        "dev": "nodemon",
        "start": "node index.js"
    }
}

also for better consideration, I have attached my hands-on project structure:

complex multi-container docker project structure

A year ago it were actually working fine, Does anyone have any idea, what’s going wrong here inside my docker-compose file now.

5

Answers


  1. Chosen as BEST ANSWER

    @Adiii yes, you are nearly right, so I have to explicitly mentioned the environment also for the postgres image but with no db parent tag.

    So here, I am explicitly mentioning the docker-compose.yaml config to help others for better understanding, also now I am using recent stable postgres image version 12-alpine, currently latest is postgres:12.3

    version: '3'
    services:
        postgres:
            image: 'postgres:12-alpine'
            environment:
                POSTGRES_PASSWORD: postgres_password
        redis:
            image: 'redis:latest'
        server:
            build:
                dockerfile: Dockerfile.dev
                context: ./server
            volumes:
                - /app/node_modules
                - ./server:/app
            environment:
                - REDIS_HOST=redis
                - REDIS_PORT=6379
                - PGUSER=postgres
                - PGHOST=postgres
                - PGDATABASE=postgres
                - PGPASSWORD=postgres_password
                - PGPORT=5432
    

    and so after docker-compose up the creating and running logs were as following:

    PS E:dockercomplex> docker-compose up
    Creating network "complex_default" with the default driver
    Creating complex_postgres_1 ... done
    Creating complex_redis_1    ... done
    Creating complex_server_1   ... done
    Attaching to complex_redis_1, complex_postgres_1, complex_server_1
    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 dynamic shared memory implementation ... posix
    postgres_1  | selecting default max_connections ... 100
    postgres_1  | selecting default shared_buffers ... 128MB
    postgres_1  | selecting default time zone ... UTC
    redis_1     | 1:C 05 Aug 2020 14:24:48.692 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis_1     | 1:C 05 Aug 2020 14:24:48.692 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
    redis_1     | 1:C 05 Aug 2020 14:24:48.692 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    postgres_1  | creating configuration files ... ok
    redis_1     | 1:M 05 Aug 2020 14:24:48.693 * Running mode=standalone, port=6379.
    redis_1     | 1:M 05 Aug 2020 14:24:48.693 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    redis_1     | 1:M 05 Aug 2020 14:24:48.694 # Server initialized
    redis_1     | 1:M 05 Aug 2020 14:24:48.694 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    redis_1     | 1:M 05 Aug 2020 14:24:48.694 * Ready to accept connections
    postgres_1  | running bootstrap script ... ok
    server_1    |
    server_1    | > @ dev /app
    server_1    | > nodemon
    server_1    |
    postgres_1  | performing post-bootstrap initialization ... sh: locale: not found
    postgres_1  | 2020-08-05 14:24:50.153 UTC [29] WARNING:  no usable system locales were found
    server_1    | [nodemon] 2.0.4
    server_1    | [nodemon] to restart at any time, enter `rs`
    server_1    | [nodemon] watching path(s): *.*
    server_1    | [nodemon] watching extensions: js,mjs,json
    server_1    | [nodemon] starting `node index.js`
    postgres_1  | ok
    server_1    | Listening
    postgres_1  | syncing data to disk ... ok
    postgres_1  |
    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  | initdb: 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  | waiting for server to start....2020-08-05 14:24:51.634 UTC [34] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
    postgres_1  | 2020-08-05 14:24:51.700 UTC [34] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    postgres_1  | 2020-08-05 14:24:51.981 UTC [35] LOG:  database system was shut down at 2020-08-05 14:24:50 UTC
    postgres_1  | 2020-08-05 14:24:52.040 UTC [34] LOG:  database system is ready to accept connections
    postgres_1  |  done
    postgres_1  | server started
    postgres_1  |
    postgres_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
    postgres_1  |
    postgres_1  | waiting for server to shut down....2020-08-05 14:24:52.121 UTC [34] LOG:  received fast shutdown request
    
    postgres_1  | 2020-08-05 14:24:52.186 UTC [34] LOG:  aborting any active transactions
    postgres_1  | 2020-08-05 14:24:52.188 UTC [34] LOG:  background worker "logical replication launcher" (PID 41) exited with exit code 1
    postgres_1  | 2020-08-05 14:24:52.188 UTC [36] LOG:  shutting down
    postgres_1  | 2020-08-05 14:24:52.669 UTC [34] 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  | 2020-08-05 14:24:52.832 UTC [1] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
    postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
    postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  listening on IPv6 address "::", port 5432
    postgres_1  | 2020-08-05 14:24:52.954 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    postgres_1  | 2020-08-05 14:24:53.136 UTC [43] LOG:  database system was shut down at 2020-08-05 14:24:52 UTC
    postgres_1  | 2020-08-05 14:24:53.194 UTC [1] LOG:  database system is ready to accept connections
    

    Hope this would help manyone.


  2. A year ago it were actually working fine, Does anyone have any idea, what’s going wrong here inside my docker-compose file now.

    Seems like you pulled the fresh image, where in the new image you should specify Postgres user password. You can look into Dockerhub, the image is update one month ago

    enter image description here

    postgress-11-alpine

      db:
        image: postgres:11-alpine
        restart: always
        environment:
          POSTGRES_PASSWORD: example
    

    As the error message is self expalinatory

    You must specify POSTGRES_PASSWORD to a non-empty value for the
            superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
    

    or POSTGRES_HOST_AUTH_METHOD=trust use this which is not recommended.

    You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
     connections without a password. This is *not* recommended.
    

    POSTGRES_PASSWORD

    This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.

    Environment Variables

    Login or Signup to reply.
  3. Adding on ArifMustafa’s answer, This worked for me.

      postgres:
        image:  'postgres:12-alpine'
        environment:
            POSTGRES_PASSWORD: mypassword
        expose:
          - 5432
        volumes: 
          - postgres_data:/var/lib/postgres/data/
    
    
    volumes:
       postgres_data:
    
    Login or Signup to reply.
  4. In my case there was an error about the POSTGRES_PASSWORD (in docker-compose.yml) and the DATABASE settings for PASSWORD in the project_level settings.py file.

    After providing a password in docker-compose.yml

      db:   # For the PostgreSQL database
        image: postgres:11
        environment:
          - POSTGRES_PASSWORD=example
    

    It is necessary to provide the same password to enable access to the POSTGRES database between the two containers (in my case the web application and the database it depended on). In the project_level settings.py:

    # Database
    # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'postgres',
            'USER': 'postgres',
            'PASSWORD': 'example',
            'HOST': 'db',
            'PORT': 5432
        }
    }
    
    
    Login or Signup to reply.
  5. To resolve the error when using the command,

    docker pull postgres
    

    You must specify POSTGRES_PASSWORD to a non-empty value for the
    superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
    or POSTGRES_HOST_AUTH_METHOD=trust use this which is not recommended.

    You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password. This is not recommended.

    Here comes the solution for this:

    $ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
    

    The default postgres user and database are created in the entrypoint with initdb.

    The postgres database is a default database meant for use by users, utilities and third party applications.

    postgresql.org/docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search