从另一个表复制权限
是否可以将用户权限从 PostgreSQL 数据库中的一个表复制到另一个表?是否只是将目标表的 pg_class.relacl 列值更新为源表的值,如下所示:
UPDATE pg_class
SET relacl=(SELECT relacl FROM pg_class WHERE relname='source_table')
WHERE relname='target_table';
这似乎可行,但我是否缺少其他可能需要的内容用这种方法可以完成还是有其他“陷阱”?
Is it possible to copy the user permissions from one table in a PostgreSQL database to another table? Is it just a matter of updating the pg_class.relacl
column value for the target table to the value for the source table, as in:
UPDATE pg_class
SET relacl=(SELECT relacl FROM pg_class WHERE relname='source_table')
WHERE relname='target_table';
This seems to work, but am I missing anything else that may need to be done or other 'gotchas' with this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pg_dump
方法很好且简单,但是,它不适用于其他架构中的表,因为输出未使用架构名称限定表。相反,它会生成:并且无法向不存在的
public.foo_table
关系授予权限。此外,如果您在不同模式中具有相同名称的关系,则需要确保仅重命名指定模式中的表。我开始根据上面的内容编写一个 bash 脚本来解决这个问题,但它开始变得有点笨拙,所以我改用了 perl。
用法:
transfer-acl old-qualified-relation=new-qualified-relation
例如
transfer-acl foo.foo_table=foo.bar_table
将在foo 上应用授权.foo_table
到foo.bar_table
。我没有实现任何 REVOKE 重写,因为我无法获取转储来发出任何内容。将其结果通过管道传输到 psql 即可。
The
pg_dump
approach is nice and simple, however, it doesn't work with tables in other schemas, as the output doesn't qualify the table with schema name. Instead it generates:and will fail to grant privileges to an nonexistent
public.foo_table
relation.Also, if you have relations with the same name in different schemas, you need to ensure that you only rename the table in the specified schema. I began to hack a bash script base on the above to take care of this but it started to become a bit unwieldy, so I switched to perl.
Usage:
transfer-acl old-qualified-relation=new-qualified-relation
e.g.
transfer-acl foo.foo_table=foo.bar_table
will apply the grants onfoo.foo_table
to thefoo.bar_table
. I didn't implement anyREVOKE
rewriting because I wasn't able to get a dump to emit any.Pipe the results of this to
psql
and you're set.如果您可以使用命令行而不是 SQL,那么更安全的方法是使用 pg_dump:
我假设是 unix 服务器。在 Windows 上,我会对文件使用 pg_dump -s ,手动编辑它,然后将其导入数据库。
也许您还需要复制对此表所拥有的序列的权限 - pg_dump 将起作用。
If you can use command-line instead of SQL then a safer approach would be to use pg_dump:
I assume a unix server. On Windows I'd use
pg_dump -s
to a file, manually edit it and then import it to a database.Maybe you'll also need to copy permissions to sequences owned by this table - pg_dump will work.