获取正在使用 database/schema/table 的用户列表

Get the list of users who are using database/schema/table

在我的项目中有一个用例,我想向用户显示有权在 postgresql 中使用 database/schema/table 的用户。假设我创建了一个数据库员工。所以我想列出正在访问该数据库的用户。模式和表相同。我试过这个:

SELECT 
    * 
FROM 
    information_schema.tables 
WHERE 
    table_schema not in ('pg_catalog', 'information_schema') AND 
    table_schema not like 'pg_toast%'

但它提供了有关当前用户有权访问的信息。我想要正在使用 database/table/schema/column.

的访问用户列表

你可以使用函数has_table_privilege()来实现你想要的。

select has_table_privilege('user_name', 'schema.table', 'select');

更多信息here

我想您需要成为超级用户才能获得所有结果。下面显示了所有 拥有 权限的用户,不一定是他们是否正在访问表。

现在您可以调整查询以将所有用户加入表列表 -

SET search_path TO public,
      schema1;

SELECT *, 
       usename,
       has_table_privilege(usename, table_schema||'.'||table_name, 'select') as has_privilege
FROM SVV_TABLES
JOIN PG_USER
ON 1=1
WHERE table_schema = 'schema1';