Django无法连接到postgres数据库,端口5432失败:您访问的页面不存在

zbdgwd5y  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(134)

我有一个名为postgres的数据库,但是当尝试连接时,它会给出以下错误输出:端口5432失败:致命:数据库“postgres”不存在。如果我改变到template 1它的工作,但我想使用postgres。
数据库列表:

  1. Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
  2. -----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
  3. postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | | libc |
  4. template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | | libc | =c/postgres +
  5. | | | | | | | postgres=CTc/postgres
  6. template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | | libc | =c/postgres +
  7. | | | | | | | postgres=CTc/postgres

Django settings.py

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.postgresql',
  4. 'NAME': 'postgres',
  5. 'USER': 'postgres',
  6. 'PASSWORD': 'postgres',
  7. 'HOST': 'db',
  8. 'PORT': 5432,
  9. }
  10. }

请注意,我使用的是Docker. docker-compose.yml

  1. version: "3.9"
  2. services:
  3. db:
  4. image: postgres
  5. volumes:
  6. - ./data/db:/var/lib/postgresql/data
  7. environment:
  8. - POSTGRES_DB=postgres
  9. - POSTGRES_USER=postgres
  10. - POSTGRES_PASSWORD=postgres
  11. web:
  12. build: .
  13. command: python manage.py runserver 0.0.0.0:8000
  14. volumes:
  15. - .:/code
  16. ports:
  17. - "8000:8000"
  18. depends_on:
  19. - db
  20. volumes:
  21. postgres_data:
mpgws1up

mpgws1up1#

从数据库中删除此端口。

  1. 'PORT': 5432, #Remove this from database because postresql has default port 5432 so we don't need to specify it.

看看能不能解决。
如果它不能解决,那么请确保您在构建Dockerfile时安装了psycopg2。

相关问题