postgresql Django在与Postgres连接时说设置.数据库配置不正确

yxyvkwin  于 2022-12-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(124)

我是Django和postgresql的新手。Django在第一步建立了一个连接,并创建了我的表,但之后只是继续告诉'设置。数据库配置不正确,请提供名称或选项['service']值。'。我真的不知道是什么问题。我将发布一些与数据库相关的代码。我的数据库正在运行,我可以通过pgAdmin看到我的表。Django版本是4。1.4和psycopg 2版本是2.9.5。
例外情况:

Exception Type: ImproperlyConfigured at /accounts/login
Exception Value: settings.DATABASES is improperly configured. Please supply the NAME or OPTIONS['service'] value.

我的settings.py

POSTGRES_HOST = os.environ.get('POSTGRES_HOST', default="")
POSTGRES_DB = os.environ.get('POSTGRES_DB', default="")
POSTGRES_USER = os.environ.get('POSTGRES_USER', default="")
POSTGRES_PASSWORD = os.environ.get('POSTGRES_PASSWORD', default="")

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

我的docker-compose.yml

version: "3.11"

x-service-volumes: &service-volumes
  - ./:/app/:rw,cached

x-database-variables: &database-variables
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres

x-app-variables: &app-variables
  <<: *database-variables
  POSTGRES_HOST: postgres

services:
  website:
    image: myproject-website:latest
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes: *service-volumes
    depends_on:
      - db_migrate
    ports:
      - "8000:8000"

  db_migrate:
    image: myproject-website:latest
    command: python3 manage.py migrate
    volumes: *service-volumes
    environment: *app-variables
    depends_on:
      - postgres
  
  postgres:
    image: postgres
    ports:
      - "5432:5432"
    environment: *database-variables
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

我检查了项目和数据库都在docker中运行,并且正在监听新的连接。我尝试将真实的值作为默认值,如下所示:第一个月

rpppsulh

rpppsulh1#

我需要改变docker-compose.yml在网站部分如下:

services:
  website:
  image: myproject-website:latest
  command: python3 manage.py runserver 0.0.0.0:8000
  volumes: *service-volumes
  depends_on:
    - db_migrate
    - postgres
  environment: *app-variables
  ports:
    - "8000:8000"

相关问题