Grant permissions to all except one table with postgresql_grant #322
Replies: 1 comment 1 reply
-
Firstly, you would grant SELECT privileges to a user on all tables. Next, you want to ensure that this privilege is granted automatically to any new tables that might be created. For this, you would alter the default privileges in the schema. You have a table, which we'll call 'sensitive_table', where you do not want users to have the SELECT privilege. For this table, you would revoke the privilege. The SQL command to do this is "REVOKE SELECT ON sensitive_table FROM myuser". Remember, altering default privileges only affects tables created after the command is issued, not tables that already exist. This is why the first command, to grant privileges to all tables, is necessary. Also, keep in mind that the REVOKE command only removes privileges granted in the same context. If you've granted privileges directly to a user, you need to revoke it from that user. If you granted privileges to a group and the user is a member of that group, you need to revoke the privileges from the group, not the user. If you're using Terraform to manage your infrastructure, you would create three different resources for these steps: one for granting privileges to all tables, one for altering default privileges, and one for revoking privileges for the special table. Make sure these resources are executed in the right order by managing their dependencies. |
Beta Was this translation helpful? Give feedback.
-
I have a use case where I would like to grant
SELECT
privileges to all of my tables except for one. I looked through the documentation, but could not find a clean way to do this. My hack was to grant privileges to all tables in one postgresql_grant and then in a separate grant, revoke all privileges from my one specific table.This leads to a few issues with terraform applies, as the inconsistent state of privileges for my table will always be altered on every apply, which causes a resource replacement on every run - not ideal.
If other people have run into this issue, please let me know how you solved it! Much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions