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?
Thanks in advance for any replies.
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.
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.