I have several tables in Postgres 9.2.23 that I need to update privileges on regularly (daily or multiple times per day). I have a master list maintained elsewhere of who can select that I use to grant privileges. However, I want to make sure that anyone who used to be on the list but has dropped off has all their privileges revoked.
My thought was to do a clean sweep with something like:
revoke all privileges on my_table from [all users/roles]
before I grant select to the authorized users. However, I have not been able to figure out how to do a bulk revoke on all users/roles like that.
I know I can generate a list of users/roles that currently have privileges and go from there, but I am hoping there is a shortcut to bypass this.
There is a way, but it is not supported and dangerous, because it directly updates the system catalogs.
As superuser, you can do the following:
That resets the permissions on the object.
The problem is that this leaves some orphaned entries in
pg_shdepend, where (among others) dependencies between users and tables are stored, so that you cannot drop a user that still has permissions on an object.If I made no mistake, you can delete them as follows:
If that seems too dangerous for you, right you are.
It is better to write a PL/pgSQL procedure that reads out
pg_class.relacl, usesaclexplodeto get the entries, loops though them and issues individualREVOKEstatements.Or simpler, just loop through all roles and revoke all privileges for everyone except for the owner.
You could safe yourself the whole mess if you don't grant privileges directly to the users, but use roles. Grant privileges to objects to the roles, and assign roles to the users.
That way it is much simpler to manage privileges.