postgresql设置的密码中有特殊字符导致Python连接URI失败
·
前言
在搭建Ubuntu Fast API的postgresql数据库时,设置密码时带入了URL中的连字符会导致Python连接失败
现象
socket.gaierror: [Errno -2] Name or service not known

原因
密码 XXX@2025 中包含 @ 字符,在数据库连接字符串中与主机分隔符冲突。
解决方案
对密码中的特殊字符进行URL编码:@ → %40
错误写法:
postgresql+asyncpg://XXX_user:XXX@2025@localhost:5432/XXX_database
↑ ↑
密码中的@ 分隔符
正确写法:
postgresql+asyncpg://XXX_user:XXX%402025@localhost:5432/XXX_database
↑
@编码为%40
常见特殊字符编码表
| 字符 | URL编码 |
|---|---|
@ | %40 |
: | %3A |
/ | %2F |
? | %3F |
# | %23 |
更多推荐



所有评论(0)