Reputation: 5222
I have a web app backed by Postgres.
user_id
column.I occasionally have bugs where I forget to add user_id = ?
to the WHERE
clause of an SQL request. To protect against this problem in a general way, I'm looking into Postgres row-level security (article):
CREATE POLICY table_policy ON table USING (user_id::TEXT = current_setting('app.user_id'))
SET app.user_id = ?
.This allows me to completely ignore user_id
when writing SELECT
and UPDATE
requests.
My remaining problem is INSERT
s. Is there a way to avoid having to provide user_id
on INSERT
s?
Upvotes: 3
Views: 264
Reputation: 7065
Just having a look at the manual :
Existing table rows are checked against the expression specified in USING, while new rows that would be created via INSERT or UPDATE are checked against the expression specified in WITH CHECK
it seems that you just have to add a WITH CHECK
clause to your policy in addition of the USING
clause, and which will apply to the INSERT and UPDATE statements.
Upvotes: 1