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

macOS Sonoma 14.5安装psycopg2-binary 2.7.7失败求助

解决macOS Sonoma 14.5下psycopg2-binary与Django 2.2的兼容及编译问题

核心问题分析

  • psycopg2-binary 2.7.7版本过旧,无法适配macOS Sonoma 14.5的系统编译环境(新版Clang导致struct _is定义不完整)
  • psycopg2-binary 2.9+版本对时区处理更严格,与Django 2.2的默认数据库配置不兼容,触发AssertionError: database connection isn't set to UTC

有效解决方案

方案1:安装兼容Django 2.2且适配Sonoma的psycopg2-binary版本

Django 2.2官方兼容psycopg2 2.5.4~2.8.x,其中2.8.6是该分支的最后稳定版,可适配Sonoma系统:

  1. 卸载现有冲突版本:
    pip uninstall -y psycopg2-binary
    
  2. 指定版本安装:
    pip install psycopg2-binary==2.8.6
    
  3. 若仍出现编译问题(预编译包适配异常),通过brew安装PostgreSQL后指定依赖路径:
    # 安装PostgreSQL依赖
    brew install postgresql
    # 带路径参数安装
    pip install psycopg2-binary==2.8.6 \
      --global-option=build_ext \
      --global-option="-I$(brew --prefix postgresql)/include" \
      --global-option="-L$(brew --prefix postgresql)/lib"
    

方案2:若需使用psycopg2-binary 2.9+(不推荐,无官方兼容保障)

修改Django数据库配置,强制数据库连接使用UTC时区:
在项目settings.py的DATABASES配置中添加OPTIONS:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '你的数据库名',
        'USER': '你的用户名',
        'PASSWORD': '你的密码',
        'HOST': 'localhost',
        'PORT': '5432',
        'OPTIONS': {
            'options': '-c timezone=UTC'
        },
    }
}

前置环境检查

确保Xcode Command Line Tools为最新版本:

xcode-select --install
sudo xcodebuild -license accept

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 08:42:43