使用密码中带有感叹号的 postgres URL 连接
using a postgres URL connection with an exclamation point in the password
我正在尝试在 bash 脚本中使用 URL 格式连接到 postgres 数据库:
psql "postgresql://larryq:password!@localhost:5432/postgres"
..然而,当我这样做时,我收到一条关于 !@localhost
事件未找到的消息。
我查看了文档,但不确定发生了什么或如何避开感叹号。我试过在它周围加上单引号,我试过在它之前加一个反斜杠,但没有任何效果。
使用相同的格式,但用户密码中没有感叹号,效果很好。我怎么能在那里感叹一下呢?
您可以将密码存储在 PGPASSWORD
中并调用 psql
:
edb=# create role foouser with password 'abc123!';
CREATE ROLE
edb=# alter user foouser with login;
ALTER ROLE
edb=> \q
[root@dba /]# PGPASSWORD='abc123!' psql "postgresql://foouser@localhost:5432/edb"
psql.bin (10.10.18)
Type "help" for help.
edb=>
或
[root@dba /]# PD='abc123!'
[root@dba /]# echo "postgresql://foouser:${PD}@localhost:5432/edb"
postgresql://foouser:abc123!@localhost:5432/edb
[root@dba /]# psql "postgresql://foouser:${PD}@localhost:5432/edb"
psql.bin (10.10.18)
Type "help" for help.
edb=>
我正在尝试在 bash 脚本中使用 URL 格式连接到 postgres 数据库:
psql "postgresql://larryq:password!@localhost:5432/postgres"
..然而,当我这样做时,我收到一条关于 !@localhost
事件未找到的消息。
我查看了文档,但不确定发生了什么或如何避开感叹号。我试过在它周围加上单引号,我试过在它之前加一个反斜杠,但没有任何效果。
使用相同的格式,但用户密码中没有感叹号,效果很好。我怎么能在那里感叹一下呢?
您可以将密码存储在 PGPASSWORD
中并调用 psql
:
edb=# create role foouser with password 'abc123!';
CREATE ROLE
edb=# alter user foouser with login;
ALTER ROLE
edb=> \q
[root@dba /]# PGPASSWORD='abc123!' psql "postgresql://foouser@localhost:5432/edb"
psql.bin (10.10.18)
Type "help" for help.
edb=>
或
[root@dba /]# PD='abc123!'
[root@dba /]# echo "postgresql://foouser:${PD}@localhost:5432/edb"
postgresql://foouser:abc123!@localhost:5432/edb
[root@dba /]# psql "postgresql://foouser:${PD}@localhost:5432/edb"
psql.bin (10.10.18)
Type "help" for help.
edb=>