ped*_*nta 220 postgresql permissions ubuntu psql
我正在进行数据库服务器迁移,但我不知道(在谷歌搜索和在这里搜索之后)如何使用psql
命令行工具列出 PostgreSQL 上的数据库权限(或服务器上的所有权限)?
我在 Ubuntu 11.04 上,我的 PostgreSQL 版本是 8.2.x。
DrC*_*sos 178
postgres=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
Run Code Online (Sandbox Code Playgroud)
Privileges 上的文档解释了如何解释输出。对于当前数据库的某个表的特定权限,请使用\z myTable
.
Jac*_*las 155
也许您的意思是列出用户及其对数据库的权限-我无法从问题中完全看出:
postgres=> \du
List of roles
Role name | Attributes | Member of
-----------------+--------------+------------------------------------------------
dba | Create role | {util_user,helpdesk_user,helpdesk_admin}
helpdesk_admin | Cannot login | {helpdesk_user}
helpdesk_user | Cannot login | {helpdesk_reader}
jack | | {helpdesk_admin}
postgres | Superuser | {}
: Create role
: Create DB
Run Code Online (Sandbox Code Playgroud)
小智 130
您可以通过以下方式做到这一点:
SELECT grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name='mytable'
Run Code Online (Sandbox Code Playgroud)
这为您提供了这种输出:
mail=# select grantee, privilege_type from information_schema.role_table_grants where table_name='aliases';
grantee | privilege_type
--------------+-----------------
mailreader | INSERT
mailreader | SELECT
mailreader | UPDATE
mailreader | DELETE
mailreader | TRUNCATE
mailreader | REFERENCES
mailreader | TRIGGER
(7 rows)
mail=#
Run Code Online (Sandbox Code Playgroud)
Vao*_*sun 30
使用psql
元命令:
https://www.postgresql.org/docs/current/static/app-psql.html
使用 Ctrl+F 浏览页面给出:
\ddp [ pattern ]
列出默认访问权限设置。
\dp [ pattern ]
列出表、视图和序列及其相关的访问权限。
\l[+] [ pattern ]
列出服务器中的数据库并显示 .... 访问权限。
上面也提到过,但在手册页上找不到“特权”一词:
\du+
对于有登录名\dg+
的角色和没有登录的角色 - 将有一个文件"Member of"
,您可以在其中找到授予角色的角色。
我在这里故意跳过函数和语言权限,在psql
手册中发现它们几乎没有被操纵(如果你确实使用了这些权限,你就不会来这里寻求建议)。对于用户定义的类型、域等也是如此 - 在元命令之后使用“+”将显示您的权限(如果适用)。
检查权限的一种极端方法是在事务中删除用户,例如:
s=# begin; drop user x;
BEGIN
Time: 0.124 ms
ERROR: role "x" cannot be dropped because some objects depend on it
DETAIL: privileges for type "SO dT"
privileges for sequence so
privileges for schema bin
privileges for table xx
privileges for table "csTest"
privileges for table tmp_x
privileges for table s1
privileges for table test
Time: 0.211 ms
s=# rollback;
ROLLBACK
Time: 0.150 ms
Run Code Online (Sandbox Code Playgroud)
当列表比 N 长时(至少在 9.3 中),带有权限列表的警告会被折叠,但您仍然可以在日志中找到它...
小智 23
当您发出\du
命令时,卧底 psql 使用波纹管查询。
SELECT r.rolname, r.rolsuper, r.rolinherit,
r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
WHERE r.rolname !~ '^pg_'
ORDER BY 1;
Run Code Online (Sandbox Code Playgroud)
小智 16
这是我的查询,由该问题的多个答案组成:
SELECT grantee AS user, CONCAT(table_schema, '.', table_name) AS table,
CASE
WHEN COUNT(privilege_type) = 7 THEN 'ALL'
ELSE ARRAY_TO_STRING(ARRAY_AGG(privilege_type), ', ')
END AS grants
FROM information_schema.role_table_grants
GROUP BY table_name, table_schema, grantee;
Run Code Online (Sandbox Code Playgroud)
这会导致这样的结果:
+------+--------------+----------------+
| user | table | grants |
+------+--------------+----------------+
| foo | schema.table | ALL |
| bar | schema.table | SELECT, INSERT |
+------+--------------+----------------+
Run Code Online (Sandbox Code Playgroud)
小智 13
一个(可能很明显)额外的步骤是成为 postgres 用户,否则你可能会收到关于角色不存在的错误。
sudo su - postgres
psql -l
Run Code Online (Sandbox Code Playgroud)
或者
psql
postgres=> \l
Run Code Online (Sandbox Code Playgroud)
小智 7
这是列出角色(被授予者)的所有权限的方法:
SELECT grantor, grantee, table_schema, table_name, privilege_type
FROM information_schema.table_privileges
WHERE grantee = 'myuser'
Run Code Online (Sandbox Code Playgroud)
会导致:
grantor | grantee | table_schema | table_name | privilege_type
----------+----------+--------------+------------+----------------
postgres | myuser | myapp | employees | INSERT
postgres | myuser | myapp | employees | SELECT
postgres | myuser | myapp | employees | UPDATE
postgres | myuser | myapp | employees | DELETE
Run Code Online (Sandbox Code Playgroud)
在 PG 10 中工作
归档时间: |
|
查看次数: |
617625 次 |
最近记录: |