Docker Compose启动PostgreSQL时自定义用户未创建求助
PostgreSQL Docker容器未创建自定义用户的排查与解决
我使用docker-compose.dev.yaml启动PostgreSQL容器,执行docker compose -f docker-compose.dev.yaml up --build后,自定义用户并未创建。尝试清理所有Docker资源(镜像、卷、容器、缓存)后问题依旧。
我的配置文件
docker-compose.dev.yaml
services: # PostgreSQL db: image: postgres:16.1-alpine restart: always env_file: - .env ports: - '5432:5432' volumes: - db:/var/lib/postgresql/data healthcheck: test: [ "CMD-SHELL", "sh -c 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'" ] interval: 10s timeout: 3s retries: 3 volumes: db:
.env文件
POSTGRES_DB=dbStartup POSTGRES_USER=dbUser POSTGRES_PASSWORD=dbPass
关键日志信息
容器初始化日志(删除卷后启动)
startup-db-1 | The files belonging to this database system will be owned by user "postgres". startup-db-1 | This user must also own the server process. startup-db-1 | startup-db-1 | The database cluster will be initialized with locale "en_US.utf8". startup-db-1 | The default database encoding has accordingly been set to "UTF8". startup-db-1 | The default text search configuration will be set to "english". startup-db-1 | startup-db-1 | Data page checksums are disabled. startup-db-1 | startup-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok startup-db-1 | creating subdirectories ... ok startup-db-1 | selecting dynamic shared memory implementation ... posix startup-db-1 | selecting default max_connections ... 100 startup-db-1 | selecting default shared_buffers ... 128MB startup-db-1 | selecting default time zone ... UTC startup-db-1 | creating configuration files ... ok startup-db-1 | running bootstrap script ... ok startup-db-1 | sh: locale: not found startup-db-1 | 2024-01-27 01:20:34.995 UTC [30] WARNING: no usable system locales were found startup-db-1 | performing post-bootstrap initialization ... ok startup-db-1 | initdb: warning: enabling "trust" authentication for local connections startup-db-1 | initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. startup-db-1 | syncing data to disk ... ok startup-db-1 | startup-db-1 | startup-db-1 | Success. You can now start the database server using: startup-db-1 | startup-db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start startup-db-1 | startup-db-1 | waiting for server to start....2024-01-27 01:20:35.690 UTC [36] LOG: starting PostgreSQL 16.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014, 64-bit startup-db-1 | 2024-01-27 01:20:35.691 UTC [36] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" startup-db-1 | 2024-01-27 01:20:35.697 UTC [39] LOG: database system was shut down at 2024-01-27 01:20:35 UTC startup-db-1 | 2024-01-27 01:20:35.702 UTC [36] LOG: database system is ready to accept connections startup-db-1 | done startup-db-1 | server started startup-db-1 | CREATE DATABASE startup-db-1 | startup-db-1 | startup-db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* startup-db-1 | startup-db-1 | 2024-01-27 01:20:35.850 UTC [36] LOG: received fast shutdown request startup-db-1 | waiting for server to shut down....2024-01-27 01:20:35.851 UTC [36] LOG: aborting any active transactions startup-db-1 | 2024-01-27 01:20:35.855 UTC [36] LOG: background worker "logical replication launcher" (PID 42) exited with exit code 1 startup-db-1 | 2024-01-27 01:20:35.859 UTC [37] LOG: shutting down startup-db-1 | 2024-01-27 01:20:35.861 UTC [37] LOG: checkpoint starting: shutdown immediate startup-db-1 | 2024-01-27 01:20:35.910 UTC [37] LOG: checkpoint complete: wrote 925 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.020 s, sync=0.025 s, total=0.051 s; sync files=301, longest=0.002 s, average=0.001 s; distance=4269 kB, estimate=4269 kB; lsn=0/191BA30, redo lsn=0/191BA30 startup-db-1 | 2024-01-27 01:20:35.919 UTC [36] LOG: database system is shut down startup-db-1 | done startup-db-1 | server stopped startup-db-1 | startup-db-1 | PostgreSQL init process complete; ready for start up. startup-db-1 | startup-db-1 | 2024-01-27 01:20:35.979 UTC [1] LOG: starting PostgreSQL 16.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014, 64-bit startup-db-1 | 2024-01-27 01:20:35.980 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 startup-db-1 | 2024-01-27 01:20:35.980 UTC [1] LOG: listening on IPv6 address "::", port 5432 startup-db-1 | 2024-01-27 01:20:35.982 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" startup-db-1 | 2024-01-27 01:20:35.987 UTC [52] LOG: database system was shut down at 2024-01-27 01:20:35 UTC startup-db-1 | 2024-01-27 01:20:35.992 UTC [1] LOG: database system is ready to accept connections
连接错误日志(Python迁移时)
startup-db-1 | 2024-01-27 02:08:30.527 UTC [44] FATAL: password authentication failed for user "startUser" startup-db-1 | 2024-01-27 02:08:30.527 UTC [44] DETAIL: Role "startUser" does not exist. startup-db-1 | Connection matched file "/var/lib/postgresql/data/pg_hba.conf" line 128: "host all all all scram-sha-256"
问题排查与解决
1. 验证环境变量加载情况
从初始化日志仅打印CREATE DATABASE、无CREATE ROLE相关输出来看,PostgreSQL初始化时可能未读取到POSTGRES_USER和POSTGRES_PASSWORD环境变量:
- 确认
.env文件与docker-compose.dev.yaml在同一目录,文件名无拼写错误(比如不要写成.env.dev)。 - 临时在
docker-compose.dev.yaml中硬编码环境变量测试,排除env文件加载问题:services: db: # 保留其他配置 environment: POSTGRES_DB: dbStartup POSTGRES_USER: dbUser POSTGRES_PASSWORD: dbPass
2. 检查容器实际环境变量
执行以下命令查看容器内的环境变量,确认是否正确加载:
docker exec -it startup-db-1 printenv
如果输出中无POSTGRES_USER、POSTGRES_PASSWORD,说明env文件未被正确加载。
3. 修正应用连接配置
错误日志显示连接使用的是startUser,但你配置的用户是dbUser,需检查Python应用的数据库连接字符串,确保用户名是dbUser而非startUser。
4. 彻底清理Docker资源
确保清理所有相关资源,避免残留数据干扰初始化:
# 停止并删除容器 docker compose -f docker-compose.dev.yaml down # 删除指定卷(替换your-project-name为实际项目名) docker volume rm your-project-name_db # 或清理所有未使用的卷(谨慎操作) docker volume prune
重新启动容器后,观察日志是否出现CREATE ROLE相关输出。
5. 修复Alpine镜像locale问题
日志中sh: locale: not found虽非用户未创建的直接原因,但可能影响初始化流程,可在docker-compose中添加命令安装locale:
services: db: # 保留其他配置 command: > sh -c "apk add --no-cache locales && sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen en_US.UTF-8 && exec postgres" environment: LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8
内容的提问来源于stack exchange,提问作者Mihail Bury
相关产品推荐
相关产品推荐

