0

My code from settings.py

   DATABASES = {
         'default': {
         'ENGINE': 'django.db.backends.postgresql',
         'NAME': 'postgres',
         'USER': 'postgres',
         'PASSWORD': 'postgres',
         'HOST': 'db',
         'PORT': 5432
         }
   }

Dockerfile

   # Pull base image
   FROM python:3.8
   # Set environment variables
   ENV PYTHONDONTWRITEBYTECODE 1
   ENV PYTHONUNBUFFERED 1
   # Set work directory
   WORKDIR /code
   # Install dependencies
   COPY Pipfile Pipfile.lock /code/
   RUN pip install pipenv && pipenv install --system
   # Copy project
   COPY . /code/

docker-compose.yml

  version: '3.8'
  services:
   web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
   db:
    image: postgres:11

when docker-compose logs on cmd, I see an error

  db_1   | Error: Database is uninitialized and superuser password is not specified.
  db_1   |        You must specify POSTGRES_PASSWORD to a non-empty value for the
  db_1   |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
  db_1   |
  db_1   |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
  db_1   |        connections without a password. This is *not* recommended.
  db_1   |
  db_1   |        See PostgreSQL documentation about "trust":
  db_1   |        https://www.postgresql.org/docs/current/auth-trust.html

If I use basic settings.py with sqlite3 It works and connecting to the server

So I am a new one on Docker, How to solve it? Thanks for your answer

It is done to me

docker-compose.yml(Edited)

  version: '3.8'
  services:
   web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
     - .:/code
    ports:
     - 8000:8000
    depends_on:
     - db
   db:
    image: postgres:11
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  volumes:
   postgres_data:

  

2 Answers 2

2

Init your postgres container with environment variables:

  db:
      image: postgres:11
      environment:
          - POSTGRES_USER=example
          - POSTGRES_PASSWORD=example
          - POSTGRES_DB=example
Sign up to request clarification or add additional context in comments.

Comments

1

You have to set the password for PostgreSQL using the environment variable POSTGRES_PASSWORD.

If you want to disable the password requirement you can do it by replacing the POSTGRES_PASSWORD : [your_password] with POSTGRES_HOST_AUTH_METHOD: trust.

job:
  build:
    docker:
      - image: circleci/postgres:9.6
        environment:
          #...
          POSTGRES_HOST_AUTH_METHOD: trust
          #...

3 Comments

Can you replace these images with actual text, that the original questioner can copy and paste?
Actually stack overflow is not give indentation to this
I am trying to do this however the first answer is worked for me

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.