Docker Compose无法连接PostgreSQL容器问题求助
PostgreSQL容器连接失败排查与解决
问题场景
我的docker-compose.yaml配置如下:
version: '3.3' services: django: build: ./project # Dockerfile路径 command: sh -c " sleep 3 && gunicorn --bind 0.0.0.0:8000 core_app.wsgi" volumes: - ./project:/project - ./project/static:/project/static - ./project/media:/project/media - ./project/logs:/project/logs expose: - 8000 env_file: - ./.env depends_on: - db db: image: postgres:13-alpine volumes: - pg_data:/var/lib/postgresql/data/ expose: - 5432 ports: - "5433:5432" env_file: - .env environment: - POSTGRES_USER=postgresuser - POSTGRES_PASSWORD=post222 - POSTGRES_DB=yandex_parser_db
无法连接到db容器内的PostgreSQL数据库,操作及错误如下:
执行docker-compose ps输出:
sudo docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------ app-content-protect_db_1 docker-entrypoint.sh postgres Up 0.0.0.0:5433->5432/tcp app-content-protect_django_1 sh -c sleep 3 && gunicorn ... Exit 3
进入db容器执行连接命令:
sudo docker exec -ti app-content-protect_db_1 bash e21385ac10e4:/# psql -h db -U postgresuser Password for user postgresuser: psql: error: FATAL: password authentication failed for user "postgresuser"
启动docker-compose时的日志错误:
db_1 | 2024-11-19 17:59:52.314 UTC [32] FATAL: password authentication failed for user "postgresuser" db_1 | 2024-11-19 17:59:52.314 UTC [32] DETAIL: Role "postgresuser" does not exist. db_1 | Connection matched pg_hba.conf line 99: "host all all all md5" django_1 | [2024-11-19 20:59:52 +0300] [9] [ERROR] Exception in worker process django_1 | Traceback (most recent call last): django_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection django_1 | self.connect() django_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner django_1 | return func(*args, **kwargs) django_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect django_1 | self.connection = self.get_new_connection(conn_params) django_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner django_1 | return func(*args, **kwargs) django_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection django_1 | connection = Database.connect(**conn_params) django_1 | File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect django_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django_1 | psycopg2.OperationalError: FATAL: password authentication failed for user "postgresuser"
解决步骤
1. 清理旧数据卷
PostgreSQL首次启动时会根据环境变量创建用户、数据库,一旦数据卷pg_data生成,后续修改环境变量不会生效。必须先删除旧数据卷:
# 停止并删除容器 docker-compose down # 删除pg_data数据卷 docker volume rm app-content-protect_pg_data
2. 解决环境变量冲突
db服务同时配置了env_file和environment,如果.env文件里存在POSTGRES_USER、POSTGRES_PASSWORD、POSTGRES_DB,会覆盖environment中的设置。二选一即可:
- 方案一:删除
env_file行,仅保留environment配置; - 方案二:把PostgreSQL相关变量移到
.env文件,删除environment块,确保变量值统一。
3. 重新启动容器
清理数据卷并调整配置后,重新构建启动:
docker-compose up -d --build
4. 验证连接
进入db容器,用正确参数连接:
docker exec -ti app-content-protect_db_1 psql -U postgresuser -d yandex_parser_db
输入密码post222即可成功连接。
5. 同步Django配置
确保Django的settings.py数据库配置与容器变量一致:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'yandex_parser_db', 'USER': 'postgresuser', 'PASSWORD': 'post222', 'HOST': 'db', 'PORT': 5432, } }
如果使用.env文件,建议从环境变量读取配置:
import os DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': os.getenv('POSTGRES_DB'), 'USER': os.getenv('POSTGRES_USER'), 'PASSWORD': os.getenv('POSTGRES_PASSWORD'), 'HOST': os.getenv('DB_HOST', 'db'), 'PORT': os.getenv('DB_PORT', 5432), } }
内容的提问来源于stack exchange,提问作者mascai
相关产品推荐
相关产品推荐

