列出 PostgreSQL 用户帐户的命令?

列出 PostgreSQL 用户帐户的命令?

createuser&dropuser命令:

createuser - define a new PostgreSQL user account
dropuser - remove a PostgreSQL user account

有没有相应的方法来列出用户帐户?

这两个命令不需要用户调用psql或了解使用它的细节。

答案1

使用psqlshell 并:

\deu[+] [PATTERN]例如:

postgres=# \deu+
      List of user mappings
 Server | User name | FDW Options 
--------+-----------+-------------
(0 rows)

对于所有用户:

postgres=# \du 
                              List of roles
 Role name  |                   Attributes                   | Member of 
------------+------------------------------------------------+-----------
 chpert.net |                                                | {}
 postgres   | Superuser, Create role, Create DB, Replication | {}

另外比如MySQL,你可以这样做:

$ psql -c "\du"
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 chpert    |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 test      |                                                | {}

答案2

要将输出限制为仅用户名,只需从 shellpsql或本地 shell/终端执行以下操作:

psql -c "SELECT u.usename AS "User Name" FROM pg_catalog.pg_user u;"

而快速方法,如前面的答案中指出的,显示 3 列;包括用户名,角色属性列表, 和成员角色组。

psql -c "\du"

最后,正如后续答案中所指出的,PostgreSQL 命令在后台运行,以便\du通过调用以下命令显示这些“默认”列:

SELECT u.usename AS "User name",
  u.usesysid AS "User ID",
  CASE WHEN u.usesuper AND u.usecreatedb THEN CAST('superuser, create
database' AS pg_catalog.text)
       WHEN u.usesuper THEN CAST('superuser' AS pg_catalog.text)
       WHEN u.usecreatedb THEN CAST('create database' AS
pg_catalog.text)
       ELSE CAST('' AS pg_catalog.text)
  END AS "Attributes"
FROM pg_catalog.pg_user u
ORDER BY 1;

(当你输入 \du 时 psql 在幕后做了什么)

最后一部分是从PostgreSQL 邮件列表

答案3

为了更简单的答案...

从 psql 内部:

\du

从航站楼:

psql -c '\du'

它帮助我记住这一点d经常被用来d描述一些东西并且是为了爵士。

答案4

SQL 解决方案通常由 psql \du 完成:

SELECT u.usename AS "User name",
  u.usesysid AS "User ID",
  CASE WHEN u.usesuper AND u.usecreatedb THEN CAST('superuser, create
database' AS pg_catalog.text)
       WHEN u.usesuper THEN CAST('superuser' AS pg_catalog.text)
       WHEN u.usecreatedb THEN CAST('create database' AS
pg_catalog.text)
       ELSE CAST('' AS pg_catalog.text)
  END AS "Attributes"
FROM pg_catalog.pg_user u
ORDER BY 1;

参考:https://www.postgresql.org/message-id/[电子邮件受保护]

相关内容