PostgreSQL user listing

耗尽温柔 提交于 2019-12-02 16:11:51

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

psql template1 -c '\du'

from the command line prompt should do. (or \du from psql prompt when you are connected to the database).

You must understand that in PostgreSQL users are per database cluster. @Michael already demonstrates how to get a list of those.

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.

To determine, whether a specific user actually has a certain privilege ('CONNECT') for a database:

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.


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.

To list roles/user

select rolname from pg_roles;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!