pgAdmin4 6.19 Docker容器启动报错:'AnonymousUser'无'username'属性
pgAdmin4 Docker 6.19启动报错
'AnonymousUser' object has no attribute 'username'且服务器配置未加载的解决方案 将pgAdmin4的Docker镜像从6.18版本升级至6.19版本后,全新启动容器时出现两个核心问题:
- 启动日志抛出错误
'AnonymousUser' object has no attribute 'username' - 预配置了8个服务器组的
servers.json文件未被加载,最终服务器列表为空
该问题在Windows 10和Ubuntu 22.04.1 LTS系统上均能重现。
版本启动日志对比
6.18版本启动日志
pgadmin4 | NOTE: Configuring authentication for SERVER mode. pgadmin4 | pgadmin4 | pgAdmin 4 - Application Initialisation pgadmin4 | ====================================== pgadmin4 | pgadmin4 | ---------- pgadmin4 | Loading servers with: pgadmin4 | User: postgres@localhost pgadmin4 | SQLite pgAdmin config: /var/lib/pgadmin/pgadmin4.db pgadmin4 | ---------- pgadmin4 | Added 8 Server Group(s) and 31 Server(s). pgadmin4 | [2023-01-17 14:05:01 +0000] [1] [INFO] Starting gunicorn 20.1.0 pgadmin4 | [2023-01-17 14:05:01 +0000] [1] [INFO] Listening at: http://[::]:5050 (1) pgadmin4 | [2023-01-17 14:05:01 +0000] [1] [INFO] Using worker: gthread pgadmin4 | [2023-01-17 14:05:01 +0000] [94] [INFO] Booting worker with pid: 94
6.19版本启动日志
pgadmin4 | NOTE: Configuring authentication for SERVER mode. pgadmin4 | pgadmin4 | pgAdmin 4 - Application Initialisation pgadmin4 | ====================================== pgadmin4 | pgadmin4 | ---------- pgadmin4 | Loading servers with: pgadmin4 | User: postgres@localhost pgadmin4 | SQLite pgAdmin config: /var/lib/pgadmin/pgadmin4.db pgadmin4 | ---------- pgadmin4 | 'AnonymousUser' object has no attribute 'username' pgadmin4 | [2023-01-17 14:08:45 +0000] [1] [INFO] Starting gunicorn 20.1.0 pgadmin4 | [2023-01-17 14:08:45 +0000] [1] [INFO] Listening at: http://[::]:5050 (1) pgadmin4 | [2023-01-17 14:08:45 +0000] [1] [INFO] Using worker: gthread pgadmin4 | [2023-01-17 14:08:45 +0000] [92] [INFO] Booting worker with pid: 92
解决思路与可行方案
1. 清理旧配置数据库,重新初始化
6.19版本可能对pgAdmin的SQLite配置数据库(pgadmin4.db)schema做了调整,旧版本的数据库文件会导致用户认证异常:
- 停止并删除现有容器:
docker stop pgadmin4 && docker rm pgadmin4 - 删除挂载的pgadmin数据目录下的
pgadmin4.db文件(比如挂载目录是/var/lib/pgadmin,则执行rm /var/lib/pgadmin/pgadmin4.db) - 重新启动6.19版本容器,确保
PGADMIN_DEFAULT_EMAIL和PGADMIN_DEFAULT_PASSWORD环境变量正确设置,待用户初始化完成后再导入服务器配置。
2. 调整服务器配置加载时机
报错本质是加载servers.json时,目标用户还未完成创建,导致系统使用匿名用户尝试操作:
- 手动导入配置:启动容器并登录pgAdmin后,右键点击服务器组 → 「Import Servers」,选择本地的
servers.json文件完成导入。 - 自定义启动脚本延迟加载:编写shell脚本等待服务启动完成后再导入配置,示例脚本:
将脚本挂载到容器内,替换默认启动命令。#!/bin/bash # 启动pgadmin服务 /entrypoint.sh & # 等待服务初始化完成 sleep 30 # 导入服务器配置 python /usr/lib/python3/dist-packages/pgadmin4/setup.py --load-servers /pgadmin4/servers.json --user postgres@localhost # 保持进程运行 wait
3. 验证servers.json格式合规性
检查servers.json是否存在语法错误(比如逗号遗漏、引号不匹配),同时确认文件中关联的用户邮箱与PGADMIN_DEFAULT_EMAIL完全一致,避免用户匹配失败。
4. 回退至6.18版本(临时方案)
如果上述方案均无法快速解决,可以暂时回退到6.18版本,等待官方修复该版本bug:
docker run -d -p 5050:80 -e PGADMIN_DEFAULT_EMAIL=postgres@localhost -e PGADMIN_DEFAULT_PASSWORD=your_password dpage/pgadmin4:6.18
内容的提问来源于stack exchange,提问作者swiss_knight
相关产品推荐
相关产品推荐

