'How to fix error "Error: Database is uninitialized and superuser password is not specified."

Hello i get this error after i run docker-compose build up

But i get this error postgres_1 | Error: Database is uninitialized and superuser password is not specified.

Here is a snap shot of the error! enter image description here

And down below is my docker-compose.yml file

version: '3.6'

Server.js file

services: 
  smart-brain-api:
    container_name: backend
    build: ./
    command: npm start
    working_dir: /usr/src/smart-brain-api
    ports:
      - "3000:3000"
    volumes:
      - ./:/usr/src/smart-brain-api
 
  #PostGres Database
 
  postgres:
    image: postgres
    ports:
      - "5432:5432"


Solution 1:[1]

To avoid that you can specify the followings environments variables for postgres container on your docker-compose file.

POSTGRES_PASSWORD This environment variable is normally required for you to use the PostgreSQL image. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.

POSTGRES_DB This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.

For more information about Environment Variables check: https://hub.docker.com/_/postgres

Solution 2:[2]

You can use the POSTGRES_HOST_AUTH_METHOD environment property by making the following change to your docker-compose.yml.

db:
    image: postgres:9.6-alpine
    environment:
      POSTGRES_DB: "db"
      POSTGRES_HOST_AUTH_METHOD: "trust"

The above will solve the error.

Solution 3:[3]

It's already mentioned in the interactive mode; how to run the container, if you don't find it, use the following:

To allow all connections without a password use:

  • docker run -e POSTGRES_HOST_AUTH_METHOD=trust postgres:9.6 (use the tag you need).

To specify postgres password for the superuser, use:

  • docker run -e POSTGRES_PASSWORD=<your_password> postgres:9.6 (use the tag you need).

Solution 4:[4]

You can make change to your docker-compose.yml file like in example:

db: image: postgres:13 environment: - "POSTGRES_HOST_AUTH_METHOD=trust"

Solution 5:[5]

Summing up the command on official docker site:

docker run --name <YOUR_POSTGRES_DB> -e POSTGRES_PASSWORD=<YOUR_POSTGRES_PASSWORD> -d postgres

Solution 6:[6]

You can make your connection using the below docker command.

docker run -e POSTGRES_PASSWORD=<your_password> postgres:9.6.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 matiferrigno
Solution 2 Laban
Solution 3 Augustine Jose
Solution 4 Fuad Palchayev
Solution 5 kaustubhd9
Solution 6 Shahbaz Rahmat