python config_error ai_generated true

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

sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'postgresql://user:pass@localhost:5432/db'

ID: python/sqlalchemy-invalid-database-url

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2025-04-01首次发现

版本兼容性

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

根因分析

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

English

The database URL is malformed, often due to special characters in the password not being URL-encoded.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. Removing special characters from the password 70% 失败

    This changes the credentials and may break authentication.

  2. Using single quotes around the URL 80% 失败

    The URL is still invalid; the issue is with encoding.