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

wgp*_*ubs 15 postgresql url connection-string postgresql-9.1

使用Postgresql URL连接字符串,格式为:

postgresql://user:secret@localhost
Run Code Online (Sandbox Code Playgroud)

如何处理该字符串中的特殊字符(例如$),以便在连接到我的postgres数据库时实际运行?

我试过简单的URL编码,所以例如,"test $"变成"test%24"......但这似乎是一个问题,因为我在尝试使用它时遇到"致命:密码验证失败"错误.

Dan*_*ité 15

See Connection URIs in the doc.

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

  • URIs are supported by postgres since version 9.2 only, so with a 9.1 client 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.

Tried with 9.3:

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

$ psql 'postgresql://daniel:p$ass@localhost/test'
Run Code Online (Sandbox Code Playgroud)

works

$ psql 'postgresql://daniel:p%24ass@localhost'
Run Code Online (Sandbox Code Playgroud)

works

psql 'postgresql://daniel:pass@localhost/test'
Run Code Online (Sandbox Code Playgroud)

fails as expected: bad password.

  • @Fjordo`@`必须以%40`的形式传入。另请参见[此问题](https://github.com/vitaly-t/connection-string/issues/15),针对要使用的正确库;) (3认同)
  • 如果特殊字符是“@”符号怎么办? (2认同)

小智 9

也许您的输入 shell 有不同的编码,但$不是%24在https://www.urlencoder.org/上查看

提示:如果您用于alter user "username" password 'p$ass''word'设置/更改密码,则必须用另一个单引号屏蔽单引号。