You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在SQLAlchemy URL中添加PostgreSQL Schema致Ubuntu密码认证失败

问题描述

我生成SQLAlchemy URL并传递给其他服务,针对PostgreSQL需要创建带Schema的URL,使用的格式是:

postgresql+psycopg2://{user_name}:{password}@{host}:{port}/{dbname}?options=-csearch_path%3D{schema_name}

该URL在Mac上可正常工作,但在Ubuntu中会出现密码认证失败错误:

connection to server at {host}, port {port} failed: FATAL:  password authentication failed for user {user_name}

仅移除URL中的Schema参数即可正常连接,尝试过将%3D替换为=、将-c替换为--均无效,且无法通过元数据设置Schema,仅能通过URL配置,请问该如何正确设置Schema?

解决方案

1. 对密码进行URL编码

Ubuntu环境下,若密码包含特殊字符(如@、=、%等),未编码的密码会和URL后续的options参数产生解析冲突,导致认证失败。需先对密码做URL编码,再拼接进URL。
例如密码为abc=123,编码后为abc%3D123,生成的完整URL示例:

postgresql+psycopg2://{user_name}:abc%3D123@{host}:{port}/{dbname}?options=-csearch_path%3D{schema_name}

2. 用引号包裹options参数值

尝试将options的参数值用单引号包裹,避免解析歧义:

postgresql+psycopg2://{user_name}:{password}@{host}:{port}/{dbname}?options='-csearch_path={schema_name}'

3. 对整个options参数值做URL编码

把options的参数值整体编码(-转%2D,=转%3D),生成的URL示例:

postgresql+psycopg2://{user_name}:{password}@{host}:{port}/{dbname}?options=%2Dcsearch_path%3D{schema_name}

内容的提问来源于stack exchange,提问作者ykoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 08:33:17