簡體   English   中英

錯誤:Django 通過 Docker 連接到 PostgreSQL

[英]ERROR: Django connect to PostgreSQL via Docker

我的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

當 docker-compose 登錄 cmd 時,我看到一個錯誤

  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

如果我使用帶有 sqlite3 的基本settings.py它可以工作並連接到服務器

所以我是Docker上的新人,怎么解決? 感謝您的回答

這是對我做的

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
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  volumes:
   postgres_data:

  

使用環境變量初始化您的 postgres 容器:

  db:
      image: postgres:11
      environment:
          - POSTGRES_USER=example
          - POSTGRES_PASSWORD=example
          - POSTGRES_DB=example

您必須使用環境變量POSTGRES_PASSWORD設置PostgreSQL的密碼。

如果要禁用密碼要求,可以通過將POSTGRES_PASSWORD: [your_password]替換為POSTGRES_HOST_AUTH_METHOD: trust來實現。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM