命令列出PostgreSQL用户帐户?


88

createuserdropuser命令:

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

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

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

Answers:


121

使用psql外壳和:

\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      |                                                | {}

22
从候机楼:psql -c '\du'
弗兰克·亨纳德

5

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

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邮件列表复制的。


2

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/1121195544.8208.242.camel@state.g2switchworks.com


2

对于一个简单的答案...

在psql中:

\du

从终端:

psql -c '\du'

它帮助我记得d经常被用来d escribe东西,üü SERS。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.