如何将本地托管的 Django 应用程序连接到 Dockerized Postgres 数据库

How to connect locally hosted Django app to Dockerized Postgres database

我正在学习 Docker 并且在几次之后,我能够 运行 一个 postgres 数据库和一个 django 应用程序在两个不同的容器中。问题是 docker,我不能使用 Pycharm 的调试工具。

所以我想 运行 我的代码没有 docker 但将数据库保留在它的容器中。

但我无法连接 Docker 化的 postgres 数据库和本地托管的 Django 应用程序。我总是有这个错误:

psycopg2.OperationalError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check_migrations()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\core\management\base.py", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
    self.build_graph()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
    return self._cursor()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
    self.ensure_connection()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
    self.connect()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
    self.connect()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError

我知道 Pycharm 的专业版允许将调试工具连接到 docker 容器,但我没有。

我看到 正好相反,但我没有找到解决我的问题的方法。

下面是我的数据库容器是如何在 docker-compose.yml 中创建的(身份验证数据仅用于本地开发):

version: "3.7"
   
services:
  db:
    container_name: customer_platform_database_local
    image: postgres
    volumes:
      - postgresPFC:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=pfcdb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    hostname: db
    expose:
      - "5433" # Publishes 5433 to other containers but NOT to host machine
    ports:
      - "5433:5433"
    command: -p 5433
    restart: always
    ...

volumes:
  postgresPFC:

settings.py 中有我的数据库设置:

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

我尝试将 HOST 参数替换为 127.0.0.1、172.23.0.1(这是数据库容器 ip,通过 docker inspect 给出),但是其中 none 有效(如果我使用 172.23.0.1 我有超时)

我在将本地 pgadmin 4 连接到容器化数据库时遇到了同样的问题,但我希望它能以同样的方式解决。

您可以简化 docker-compose.yml 上的映射端口,将 5432 重新映射到 5433 到主机。

使用此配置,我可以使用 dbeaver

连接到 pfcd 数据库
version: "3.7"
   
services:
  db:
    container_name: customer_platform_database_local
    image: postgres
 volumes:
      - postgresPFC:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=pfcdb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    hostname: db
    ports:
      - 5433:5432
    restart: always
volumes:
  postgresPFC:

对于连接错误,我怀疑错误是在驱动程序名称上 不是 django.db.backends.postgresqldjango.db.backends.postgresql_psycopg2 注意最后的 psycopg2

this article and the official django host docs

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'pfcdb',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': 5433,
    }
}

您还必须检查您的主机上是否有其他服务或 Postgres 实例 运行 在同一端口 5433 上。