如何处理 Postgresql URL 连接字符串密码中的特殊字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23353623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:25:57  来源:igfitidea点击:

How to handle special characters in the password of a Postgresql URL connection string?

postgresqlurlconnection-stringpostgresql-9.1

提问by wgpubs

Using a Postgresql URL connection string in the format of:

使用以下格式的 Postgresql URL 连接字符串:

postgresql://user:secret@localhost

How do I handle special characters in that string (e.g., $) so that it will actually function when I connect to my postgres database?

我如何处理该字符串中的特殊字符(例如$),以便在我连接到我的 postgres 数据库时它会真正起作用?

I've tried simply URL encoding it, so for example, "test$" becomes "test%24" ... but that seems to be a problem as I get a "FATAL: password authentication failed " error when attempting to use it.

我试过简单地对它进行 URL 编码,例如,“test$”变成了“test%24”……但这似乎是一个问题,因为我在尝试使用它时收到“致命:密码验证失败”错误.

回答by Daniel Vérité

See Connection URIsin the doc.

请参阅文档中的连接 URI

There are a few things that don't seem quite right in your question:

在您的问题中,有几件事似乎不太正确:

  • URIs are supported by postgres since version 9.2only, so with a 9.1client that's not supposed to work at all. Or you're using a client that implements connection URIs itself.

  • Percent-sign encoding is supported. Per doc:

    Percent-encoding may be used to include symbols with special meaning in any of the URI parts.

  • Percent-encoding is not even necessary for a dollar character.

  • URI9.2仅从版本开始就由 postgres 支持,因此对于9.1根本不应该工作的客户端。或者您正在使用自己实现连接 URI 的客户端。

  • 支持百分号编码。每个文档:

    百分比编码可用于在任何 URI 部分中包含具有特殊含义的符号。

  • 美元字符甚至不需要百分比编码。

Tried with 9.3:

用 9.3 试过:

sql> alter user daniel password 'p$ass';

sql> alter user daniel password 'p$ass';

$ psql 'postgresql://daniel:p$ass@localhost/test'

works

作品

$ psql 'postgresql://daniel:p%24ass@localhost'

works

作品

psql 'postgresql://daniel:pass@localhost/test'

fails as expected: bad password.

按预期失败:密码错误。