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

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

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

Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
postgres  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 |            | libc            |
template0 | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 |            | libc            | =c/postgres          +
          |          |          |             |             |            |                 | postgres=CTc/postgres
template1 | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 |            | libc            | =c/postgres          +
          |          |          |             |             |            |                 | postgres=CTc/postgres

Django settings.py

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

 }
}

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

version: "3.9"

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

mpgws1up1#

从数据库中删除此端口。

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

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

相关问题