python network_error ai_generated true

psycopg2 操作错误:需要 SSL 连接,请指定 SSL 选项后重试。

psycopg2.OperationalError: SSL connection is required. Please specify SSL options and retry.

ID: python/psycopg2-ssl-connection-error

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-03-22首次发现

版本兼容性

版本状态引入弃用备注
2.9.x active

根因分析

数据库服务器配置了强制 SSL,但客户端连接时未提供 SSL 参数。

English

数据库服务器配置了强制 SSL,但客户端连接时未提供 SSL 参数。

generic

解决方案

  1. 90% 成功率
    在连接字符串中添加 sslmode=require,例如:postgresql://user:pass@host/db?sslmode=require
  2. 95% 成功率
    使用 SQLAlchemy 的 connect_args 参数:create_engine(url, connect_args={'sslmode': 'require'})

无效尝试

常见但无效的做法:

  1. 50% 失败

    尝试关闭服务器端的 SSL 要求,但可能影响其他生产应用的安全性。

  2. 80% 失败

    在连接字符串中直接添加 sslmode=disable,但服务器会拒绝非 SSL 连接。