You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DigitalOcean App Platform部署Django时gunicorn worker异常求助

Django部署到DigitalOcean App Platform后Gunicorn启动失败问题解决

问题描述

按照DigitalOcean应用平台文档部署已有GitHub仓库的Django应用,部署流程显示成功,但运行日志出现Gunicorn相关错误,核心报错为SystemError: initialization of _psycopg raised unreported exception,完整日志如下:

[vksite] [2023-12-19 05:44:29] [2023-12-19 05:44:29 0000] 1 [INFO]
Starting gunicorn 21.2.0 [vksite] [2023-12-19 05:44:29] [2023-12-19
05:44:29 0000] 1 [INFO] Listening at: http://0.0.0.0:8080 (1)
[vksite] [2023-12-19 05:44:29] [2023-12-19 05:44:29 0000] 1 [INFO]
Using worker: sync [vksite] [2023-12-19 05:44:29] [2023-12-19 05:44:29
0000] [14] [INFO] Booting worker with pid: 14 [vksite] [2023-12-19
05:44:31] [2023-12-19 05:44:31 0000] [14] [ERROR] Exception in worker
process ........ [vksite] [2023-12-19 05:44:31] SystemError:
initialization of _psycopg raised unreported exception [vksite]
[2023-12-19 05:44:31] [2023-12-19 05:44:31 0000] [14] [INFO] Worker
exiting (pid: 14) [vksite] [2023-12-19 05:44:31] [2023-12-19 05:44:31
0000] 1 [ERROR] Worker (pid:14) exited with code 3 [vksite]
[2023-12-19 05:44:31] [2023-12-19 05:44:31 0000] 1 [ERROR] Shutting
down: Master [vksite] [2023-12-19 05:44:31] [2023-12-19 05:44:31 0000]
1 [ERROR] Reason: Worker failed to boot.

解决办法

  • 检查Psycopg版本兼容性
    确保使用的Psycopg版本与Python、Django版本匹配:

    • 若使用Python 3.10及以上版本,优先选择psycopg[binary](Psycopg3)或psycopg2-binary的稳定版,避免编译问题。
    • 在requirements.txt中明确指定版本,例如:
      psycopg2-binary==2.9.9
      # 或Psycopg3版本
      psycopg[binary]==3.1.17
      
  • 确认依赖安装完整性

    • 如果使用的是psycopg2(非binary版本),需确保部署环境已安装libpq-dev等编译依赖;使用binary版本可跳过此步骤。
    • 本地先执行pip install -r requirements.txt验证依赖是否能正常安装,排除本地与部署环境的差异。
  • 核对数据库配置

    • 检查Djangosettings.py中的数据库配置,确认数据库URL、用户名、密码与DigitalOcean提供的数据库实例信息完全一致,无拼写错误。
    • 确认数据库实例已正常运行,且App Platform应用已被添加到数据库的访问白名单中。
  • 清理缓存重新部署

    • 删除仓库中的__pycache__目录、Pipfile.lock(若用Pipenv)等缓存文件,重新推送代码触发部署。
    • 在DigitalOcean App Platform控制台选择"重新构建"应用,确保依赖是全新安装的。

内容的提问来源于stack exchange,提问作者Santana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 18:41:23