简体   繁体   English

从主机上运行的 django 连接到 docker 容器内的 postgres 数据库

[英]connect to a postgres database inside of a docker container from django running on host machine

I have a postgres database running inside a container with pgadmin connected to it, the docker-compose.yml is as follows:我有一个在容器内运行的 postgres 数据库,pgadmin 连接到它,docker-compose.yml 如下:

postgres:
  image: postgres:13.0-alpine
  volumes:
    - postgres:/var/lib/postgresql/data
  ports:
    - "5432:5432"
  env_file:
    - $ENV_FILE

pgadmin:
  image: dpage/pgadmin4
  volumes:
    - pgadmin:/var/lib/pgadmin
  ports:
    - "${PGADMIN_PORT:-5050}:80"
  restart: unless-stopped
  depends_on:
    - postgres
  env_file:
    - $ENV_FILE

my django database settings are:我的 django 数据库设置是:

DATABASES = {
    "default": {
        "ENGINE": os.environ.get("POSTGRES_ENGINE", "django.db.backends.postgresql"),
        "NAME": os.environ.get("POSTGRES_NAME", "postgres"),
        "USER": os.environ.get("POSTGRES_USER", "admin"),
        "PASSWORD": os.environ.get("POSTGRES_PASS", "admin"),
        "HOST": os.environ.get("POSTGRES_HOST", "127.0.0.1"),
        "PORT": os.environ.get("POSTGRES_PORT", "5432"),
    }
}

The traceback is:追溯是:

    Traceback (most recent call last):
  File "C:\Users\liam.obrien\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\liam.obrien\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\channels\management\commands\runserver.py", line 76, in inner_run
    self.check_migrations()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\core\management\base.py", line 576, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
    self.build_graph()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
    if self.has_table():
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
    return self._cursor()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
    self.ensure_connection()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 243, in ensure_connection
    with self.wrap_database_errors:
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
    self.connect()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\postgresql\base.py", line 203, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\psycopg2\__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "127.0.0.1", port 5432 failed: FATAL:  password authentication failed for user "admin"

Absolutely no idea what is going on as my env file has the exact same password in it as the settings.py in my django project.完全不知道发生了什么,因为我的 env 文件中的密码与我的 django 项目中的 settings.py 完全相同。

To be clear, i am running the postgres containers in docker and then trying to run the django test server locally on the host machine, and the error is thrown on startup.需要明确的是,我在 docker 中运行 postgres 容器,然后尝试在主机本地运行 django 测试服务器,启动时抛出错误。

So for anyone that finds this, i was helped along hugely by this thread;所以对于任何发现这个的人来说,这个线程对我有很大帮助; Connecting to Postgresql in a docker container from outside 从外部连接到 docker 容器中的 Postgresql

The problem ended up being that for some reason port 5432 was in use, i think by django itself (although if anyone knows the real answer that knowledge would be greatly appreciated), and the solution was to change the port mapping to another port, in my case:问题最终是由于某种原因端口 5432 正在使用中,我认为是 django 本身(尽管如果有人知道真正的答案,知识将不胜感激),解决方案是将端口映射更改为另一个端口,在我的情况:

postgres:
  ports:
    - "6543:5432"

you can then test if you can acces your postgres db from a terminal on your host machine through a uri as follows:然后,您可以测试是否可以通过 uri 从主机上的终端访问您的 postgres 数据库,如下所示:

psql postgresql://<postgres_user_name>:<postgres_pass>@localhost:6543/postgres

another problem i ran into is that to get the psql command, you need to add postgres to your system environment variables path as follows: %PROGRAMFILES%/PostgreSQL/<postgres_version_num>/bin/我遇到的另一个问题是要获取psql命令,您需要将 postgres 添加到系统环境变量路径中,如下所示: %PROGRAMFILES%/PostgreSQL/<postgres_version_num>/bin/

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

相关问题 带有Python Web App的Docker容器-在主机(OSX)上连接到Postgres - Docker container with Python Web App - Connect to Postgres on Host machine (OSX) 无法将 django 容器连接到 linux 主机上的 postgres - Cannot connect django container to postgres on a linux host machine 从主机连接到运行MongoDB的Docker容器 - Connect from host to Docker container running MongoDB 从 docker 容器连接主机上运行的服务 - connect with service running on host from docker container 在本地计算机上运行的Django无法连接到运行MYSQL的Docker容器 - django running on local machine cannot connect to docker container running MYSQL 尝试从主机连接到docker容器内的python套接字 - Trying to connect to a python socket inside a docker container from host 无法从 Flask 连接到 Postgres Docker 容器 - Unable to Connect from Flask to Postgres Docker Container 无法从我的 docker 容器连接到远程数据库实例,但是可以从我的主机连接 - Cannot connect to remote Database instance from my docker container, however can connect from my host computer 如何从运行在同一台机器上的 Docker 内部加载运行在主机中的 MongoDB 中的数据? - How to load data in MongoDB running in host from inside a Docker running on the same machine? 无法连接到 docker 容器内的 postgre 数据库 - Failed to connect to postgre database inside docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM