[英]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.