PostgreSQL 用户列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8926389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL user listing
提问by Arun Dambal
I want to get a list of users for a certain database in psql - for example "template0". Who are the users? Or for "template1" database: - who are the users there?
我想在 psql 中获取某个数据库的用户列表 - 例如“template0”。用户是谁?或者对于“template1”数据库: - 那里的用户是谁?
Already tried:
已经尝试过:
\du+ -- no database is Listed not Users
Select * from "pg_users"; -- no database is listed
回答by Michael Krelin - hacker
User aren't actually, "for the database", they're for cluster and are given different permissions to access databases. To list users \du
should do, but you need to be connected. Something like
用户实际上不是“用于数据库”,而是用于集群并被授予不同的访问数据库的权限。列出用户\du
应该做的,但你需要连接。就像是
psql template1 -c '\du'
from the command line prompt should do. (or \du
from psql prompt when you are connected to the database).
从命令行提示符应该做。(或\du
从 psql 提示连接到数据库时)。
回答by Erwin Brandstetter
You must understand that in PostgreSQL users are per database cluster. @Michael already demonstrates how to get a list of those.
您必须了解,在 PostgreSQL 中,用户是按数据库集群划分的。@Michael 已经演示了如何获取这些列表。
So, unless you restrict permissions for a particular databases explicitly with REVOKE
and GRANT
, all users in the cluster have basic access to any database in the cluster.
因此,除非您使用REVOKE
和明确限制对特定数据库的权限,否则GRANT
集群中的所有用户都具有对集群中任何数据库的基本访问权限。
To determine, whether a specific useractually has a certain privilege ('CONNECT') for a database:
要确定特定用户是否确实具有数据库的特定权限 ('CONNECT'):
has_database_privilege(user, database, privilege)
More about privilege functions in the manual.
To determine all specific privileges for a specific database:
要确定特定数据库的所有特定权限:
SELECT datname, datacl
FROM pg_database
WHERE datname = 'mydb';
You get NULL
for datacl
if no specific restrictions apply.
你得到NULL
的datacl
,如果没有具体的限制。
In addition to that you can restrict access per database and per user in the pg_hba.conf
file. That's on a lower level. The user cannot even connect, if pg_hba.conf
won't let him, even if the database itself would allow access.
除此之外,您可以限制pg_hba.conf
文件中每个数据库和每个用户的访问。那是在较低的水平上。用户甚至无法连接,如果pg_hba.conf
不让他,即使数据库本身允许访问。
回答by Sachin Gupta
To list roles/user
列出角色/用户
select rolname from pg_roles;
从 pg_roles 中选择 rolname;