你如何在PostgreSQL中更改用户?

Abh*_*han 6 postgresql psql

postgres=# \du
                         List of roles
Role name |                   Attributes                   | Member of 

-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 shorturl  | Superuser                                      | {}

postgres=# create database shorturl;
CREATE DATABASE

postgres=# \c shorturl;
You are now connected to database "shorturl" as user "postgres".

shorturl=# 
Run Code Online (Sandbox Code Playgroud)

我的偏好是使用数据库shorturl是用户shorturl而不是postgres.我该如何改变呢?

提前致谢.

a_h*_*ame 18

当您psql通过输入显示在线帮助时,\?您可以看到:

Connection
  \c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
                         connect to new database (currently "postgres")
Run Code Online (Sandbox Code Playgroud)

所以你需要使用:

\c shorturl shorturl
Run Code Online (Sandbox Code Playgroud)


Ron*_*ohn 6

如果您已经连接到数据库shorturl,另一个选项是命令:

SET ROLE shorturl;
Run Code Online (Sandbox Code Playgroud)

该命令的好处是它不与 psql 绑定,并且您可以更改工作的“中游”用户。