# sqlalchemy.exc.ArgumentError: 无法从字符串 'postgresql://user:pass@localhost:5432/db' 解析 rfc1738 URL

- **ID:** `python/sqlalchemy-invalid-database-url`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

数据库 URL 格式错误，通常是由于密码中的特殊字符未进行 URL 编码。

## 版本兼容性

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

## 解决方案

1. **URL-encode the password** (98% 成功率)
   ```
   from urllib.parse import quote_plus
password = 'p@ssword!'
encoded_password = quote_plus(password)
url = f'postgresql://user:{encoded_password}@localhost:5432/db'
   ```
2. **Use a config file with properly escaped strings** (90% 成功率)
   ```
   # In config file:
DATABASE_URL=postgresql://user:p%40ssword%21@localhost:5432/db
   ```

## 无效尝试

- **Removing special characters from the password** — This changes the credentials and may break authentication. (70% 失败率)
- **Using single quotes around the URL** — The URL is still invalid; the issue is with encoding. (80% 失败率)
