# psycopg2 操作错误：需要 SSL 连接，请指定 SSL 选项后重试。

- **ID:** `python/psycopg2-ssl-connection-error`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2.9.x | active | — | — |

## 解决方案

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

## 无效尝试

- **** — 尝试关闭服务器端的 SSL 要求，但可能影响其他生产应用的安全性。 (50% 失败率)
- **** — 在连接字符串中直接添加 sslmode=disable，但服务器会拒绝非 SSL 连接。 (80% 失败率)
