简体   繁体   English

将 Django 连接到 Docker Postgres 实例引发 django.db.utils.OperationalError

[英]Connecting Django to Docker Postgres instance raising django.db.utils.OperationalError

I'm trying to execute a django project on my local machine, the project requires Postgres.我正在尝试在我的本地机器上执行 django 项目,该项目需要 Postgres。

I know close to nothing of docker.我对 docker 几乎一无所知。 I pulled postgres image from docker hub and executed the followin command, as suggested by the instructions in postgres docker hub page.我从 docker 集线器中提取了 postgres 图像,并按照 postgres docker 集线器页面中的说明执行了以下命令。

$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

The docker container is up: docker 容器已启动:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
402180487f68        postgres            "docker-entrypoint.s…"   2 hours ago         Up 2 hours          5432/tcp            some-postgres

But I can't make Django to connect to it.但我无法让 Django 连接到它。 (Django is running on my local machine, not on docker) Django settings: (Django 在我的本地机器上运行,而不是在 docker 上) Django 设置:

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

If I execute migrations with the settings above the error is:如果我使用上述设置执行迁移,错误是:

django.db.utils.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

I assume the connection is not beign refused, because if I stop the container the error is the same.我假设连接没有被拒绝,因为如果我停止容器,错误是一样的。

Some tutorial/answer suggested HOST should be the container name.一些教程/答案建议 HOST 应该是容器名称。 To me it doesn't make much sense, as I don't know how Django is supposed to resolve that, but I tried nonetheless:对我来说这没有多大意义,因为我不知道 Django 应该如何解决这个问题,但我还是尝试了:

        'HOST': 'some-postgres',

The error raised is:引发的错误是:

django.db.utils.OperationalError: could not translate host name "some-postgres" to address: nodename nor servname provided, or not known

I have checked several questions and tutorials, but they all seem to use docker-composer and/or have the django project also inside docker.我检查了几个问题和教程,但它们似乎都使用 docker-composer 和/或在 docker 中也有 django 项目。 Still haven't been able to make the project connect to postgres.仍然无法使项目连接到 postgres。

I believe you have to forward port 5432 from the docker: https://docs.docker.com/config/containers/container-networking/#published-ports我相信您必须从 docker 转发端口 5432: https://docs.docker.com/config/containers/container-networking

Good analogy is a webserver - imagine you would start a django application in a container on port 8000. You couldn't just simply open firefox and navigate to localhost:8000 from within the host as the application is running in an isolated environment.一个很好的类比是一个网络服务器 - 假设您将在端口 8000 上的容器中启动 django 应用程序。您不能简单地打开 firefox 并从主机内导航到 localhost:8000,因为应用程序在隔离环境中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Django 连接到 Postgres:django.db.utils.OperationalError:致命:数据库“DATABASENAME”不存在 - Connecting Django to Postgres: django.db.utils.OperationalError: FATAL: database "DATABASENAME" does not exist django.db.utils.OperationalError: 没有这样的表 Django 2 - django.db.utils.OperationalError: no such table Django 2 Django:django.db.utils.OperationalError: 没有这样的列 - Django:django.db.utils.OperationalError: no such column django.db.utils.OperationalError:没有这样的列: - django.db.utils.OperationalError: no such column: django.db.utils.OperationalError的解决方案 - solution for django.db.utils.OperationalError 迁移错误(django.db.utils.OperationalError) - Migration error(django.db.utils.OperationalError) django.db.utils.OperationalError: 没有这样的表 - django.db.utils.OperationalError: no such table PythonAnywhere:django.db.utils.OperationalError:没有这样的表: - PythonAnywhere: django.db.utils.OperationalError: no such table: Django Docker:django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称或服务未知 - Django Docker: django.db.utils.OperationalError: could not translate host name “db” to address: Name or service not known Django 2.2 - django.db.utils.OperationalError: 没有这样的表 - Django 2.2 - django.db.utils.OperationalError: no such table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM