1CREATE POLICY(7) PostgreSQL 14.3 Documentation CREATE POLICY(7)
2
3
4
6 CREATE_POLICY - define a new row-level security policy for a table
7
9 CREATE POLICY name ON table_name
10 [ AS { PERMISSIVE | RESTRICTIVE } ]
11 [ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
12 [ TO { role_name | PUBLIC | CURRENT_ROLE | CURRENT_USER | SESSION_USER } [, ...] ]
13 [ USING ( using_expression ) ]
14 [ WITH CHECK ( check_expression ) ]
15
17 The CREATE POLICY command defines a new row-level security policy for a
18 table. Note that row-level security must be enabled on the table (using
19 ALTER TABLE ... ENABLE ROW LEVEL SECURITY) in order for created
20 policies to be applied.
21
22 A policy grants the permission to select, insert, update, or delete
23 rows that match the relevant policy expression. Existing table rows are
24 checked against the expression specified in USING, while new rows that
25 would be created via INSERT or UPDATE are checked against the
26 expression specified in WITH CHECK. When a USING expression returns
27 true for a given row then that row is visible to the user, while if
28 false or null is returned then the row is not visible. When a WITH
29 CHECK expression returns true for a row then that row is inserted or
30 updated, while if false or null is returned then an error occurs.
31
32 For INSERT and UPDATE statements, WITH CHECK expressions are enforced
33 after BEFORE triggers are fired, and before any actual data
34 modifications are made. Thus a BEFORE ROW trigger may modify the data
35 to be inserted, affecting the result of the security policy check.
36 WITH CHECK expressions are enforced before any other constraints.
37
38 Policy names are per-table. Therefore, one policy name can be used for
39 many different tables and have a definition for each table which is
40 appropriate to that table.
41
42 Policies can be applied for specific commands or for specific roles.
43 The default for newly created policies is that they apply for all
44 commands and roles, unless otherwise specified. Multiple policies may
45 apply to a single command; see below for more details. Table 281
46 summarizes how the different types of policy apply to specific
47 commands.
48
49 For policies that can have both USING and WITH CHECK expressions (ALL
50 and UPDATE), if no WITH CHECK expression is defined, then the USING
51 expression will be used both to determine which rows are visible
52 (normal USING case) and which new rows will be allowed to be added
53 (WITH CHECK case).
54
55 If row-level security is enabled for a table, but no applicable
56 policies exist, a “default deny” policy is assumed, so that no rows
57 will be visible or updatable.
58
60 name
61 The name of the policy to be created. This must be distinct from
62 the name of any other policy for the table.
63
64 table_name
65 The name (optionally schema-qualified) of the table the policy
66 applies to.
67
68 PERMISSIVE
69 Specify that the policy is to be created as a permissive policy.
70 All permissive policies which are applicable to a given query will
71 be combined together using the Boolean “OR” operator. By creating
72 permissive policies, administrators can add to the set of records
73 which can be accessed. Policies are permissive by default.
74
75 RESTRICTIVE
76 Specify that the policy is to be created as a restrictive policy.
77 All restrictive policies which are applicable to a given query will
78 be combined together using the Boolean “AND” operator. By creating
79 restrictive policies, administrators can reduce the set of records
80 which can be accessed as all restrictive policies must be passed
81 for each record.
82
83 Note that there needs to be at least one permissive policy to grant
84 access to records before restrictive policies can be usefully used
85 to reduce that access. If only restrictive policies exist, then no
86 records will be accessible. When a mix of permissive and
87 restrictive policies are present, a record is only accessible if at
88 least one of the permissive policies passes, in addition to all the
89 restrictive policies.
90
91 command
92 The command to which the policy applies. Valid options are ALL,
93 SELECT, INSERT, UPDATE, and DELETE. ALL is the default. See below
94 for specifics regarding how these are applied.
95
96 role_name
97 The role(s) to which the policy is to be applied. The default is
98 PUBLIC, which will apply the policy to all roles.
99
100 using_expression
101 Any SQL conditional expression (returning boolean). The conditional
102 expression cannot contain any aggregate or window functions. This
103 expression will be added to queries that refer to the table if
104 row-level security is enabled. Rows for which the expression
105 returns true will be visible. Any rows for which the expression
106 returns false or null will not be visible to the user (in a
107 SELECT), and will not be available for modification (in an UPDATE
108 or DELETE). Such rows are silently suppressed; no error is
109 reported.
110
111 check_expression
112 Any SQL conditional expression (returning boolean). The conditional
113 expression cannot contain any aggregate or window functions. This
114 expression will be used in INSERT and UPDATE queries against the
115 table if row-level security is enabled. Only rows for which the
116 expression evaluates to true will be allowed. An error will be
117 thrown if the expression evaluates to false or null for any of the
118 records inserted or any of the records that result from the update.
119 Note that the check_expression is evaluated against the proposed
120 new contents of the row, not the original contents.
121
122 Per-Command Policies
123 ALL
124 Using ALL for a policy means that it will apply to all commands,
125 regardless of the type of command. If an ALL policy exists and more
126 specific policies exist, then both the ALL policy and the more
127 specific policy (or policies) will be applied. Additionally, ALL
128 policies will be applied to both the selection side of a query and
129 the modification side, using the USING expression for both cases if
130 only a USING expression has been defined.
131
132 As an example, if an UPDATE is issued, then the ALL policy will be
133 applicable both to what the UPDATE will be able to select as rows
134 to be updated (applying the USING expression), and to the resulting
135 updated rows, to check if they are permitted to be added to the
136 table (applying the WITH CHECK expression, if defined, and the
137 USING expression otherwise). If an INSERT or UPDATE command
138 attempts to add rows to the table that do not pass the ALL policy's
139 WITH CHECK expression, the entire command will be aborted.
140
141 SELECT
142 Using SELECT for a policy means that it will apply to SELECT
143 queries and whenever SELECT permissions are required on the
144 relation the policy is defined for. The result is that only those
145 records from the relation that pass the SELECT policy will be
146 returned during a SELECT query, and that queries that require
147 SELECT permissions, such as UPDATE, will also only see those
148 records that are allowed by the SELECT policy. A SELECT policy
149 cannot have a WITH CHECK expression, as it only applies in cases
150 where records are being retrieved from the relation.
151
152 INSERT
153 Using INSERT for a policy means that it will apply to INSERT
154 commands. Rows being inserted that do not pass this policy will
155 result in a policy violation error, and the entire INSERT command
156 will be aborted. An INSERT policy cannot have a USING expression,
157 as it only applies in cases where records are being added to the
158 relation.
159
160 Note that INSERT with ON CONFLICT DO UPDATE checks INSERT policies'
161 WITH CHECK expressions only for rows appended to the relation by
162 the INSERT path.
163
164 UPDATE
165 Using UPDATE for a policy means that it will apply to UPDATE,
166 SELECT FOR UPDATE and SELECT FOR SHARE commands, as well as
167 auxiliary ON CONFLICT DO UPDATE clauses of INSERT commands. Since
168 UPDATE involves pulling an existing record and replacing it with a
169 new modified record, UPDATE policies accept both a USING expression
170 and a WITH CHECK expression. The USING expression determines which
171 records the UPDATE command will see to operate against, while the
172 WITH CHECK expression defines which modified rows are allowed to be
173 stored back into the relation.
174
175 Any rows whose updated values do not pass the WITH CHECK expression
176 will cause an error, and the entire command will be aborted. If
177 only a USING clause is specified, then that clause will be used for
178 both USING and WITH CHECK cases.
179
180 Typically an UPDATE command also needs to read data from columns in
181 the relation being updated (e.g., in a WHERE clause or a RETURNING
182 clause, or in an expression on the right hand side of the SET
183 clause). In this case, SELECT rights are also required on the
184 relation being updated, and the appropriate SELECT or ALL policies
185 will be applied in addition to the UPDATE policies. Thus the user
186 must have access to the row(s) being updated through a SELECT or
187 ALL policy in addition to being granted permission to update the
188 row(s) via an UPDATE or ALL policy.
189
190 When an INSERT command has an auxiliary ON CONFLICT DO UPDATE
191 clause, if the UPDATE path is taken, the row to be updated is first
192 checked against the USING expressions of any UPDATE policies, and
193 then the new updated row is checked against the WITH CHECK
194 expressions. Note, however, that unlike a standalone UPDATE
195 command, if the existing row does not pass the USING expressions,
196 an error will be thrown (the UPDATE path will never be silently
197 avoided).
198
199 DELETE
200 Using DELETE for a policy means that it will apply to DELETE
201 commands. Only rows that pass this policy will be seen by a DELETE
202 command. There can be rows that are visible through a SELECT that
203 are not available for deletion, if they do not pass the USING
204 expression for the DELETE policy.
205
206 In most cases a DELETE command also needs to read data from columns
207 in the relation that it is deleting from (e.g., in a WHERE clause
208 or a RETURNING clause). In this case, SELECT rights are also
209 required on the relation, and the appropriate SELECT or ALL
210 policies will be applied in addition to the DELETE policies. Thus
211 the user must have access to the row(s) being deleted through a
212 SELECT or ALL policy in addition to being granted permission to
213 delete the row(s) via a DELETE or ALL policy.
214
215 A DELETE policy cannot have a WITH CHECK expression, as it only
216 applies in cases where records are being deleted from the relation,
217 so that there is no new row to check.
218
219 Table 281. Policies Applied by Command Type
220 ┌─────────────┬─────────────┬────────────┬─────────────────────────┬────────────┐
221 │ │ SELECT/ALL │ INSERT/ALL │ UPDATE/ALL policy │ DELETE/ALL │
222 │ │ policy │ policy │ │ policy │
223 │Command ├─────────────┼────────────┼────────────┬────────────┼────────────┤
224 │ │ USING │ WITH CHECK │ USING │ WITH CHECK │ USING │
225 │ │ expression │ expression │ expression │ expression │ expression │
226 ├─────────────┼─────────────┼────────────┼────────────┼────────────┼────────────┤
227 │SELECT │ Existing │ — │ — │ — │ — │
228 │ │ row │ │ │ │ │
229 ├─────────────┼─────────────┼────────────┼────────────┼────────────┼────────────┤
230 │SELECT FOR │ Existing │ — │ Existing │ — │ — │
231 │UPDATE/SHARE │ row │ │ row │ │ │
232 ├─────────────┼─────────────┼────────────┼────────────┼────────────┼────────────┤
233 │INSERT │ — │ New row │ — │ — │ — │
234 ├─────────────┼─────────────┼────────────┼────────────┼────────────┼────────────┤
235 │INSERT ... │ New row [a] │ New row │ — │ — │ — │
236 │RETURNING │ │ │ │ │ │
237 ├─────────────┼─────────────┼────────────┼────────────┼────────────┼────────────┤
238 │UPDATE │ Existing & │ — │ Existing │ New row │ — │
239 │ │ new rows │ │ row │ │ │
240 │ │ [a] │ │ │ │ │
241 ├─────────────┼─────────────┼────────────┼────────────┼────────────┼────────────┤
242 │DELETE │ Existing │ — │ — │ — │ Existing │
243 │ │ row [a] │ │ │ │ row │
244 ├─────────────┼─────────────┼────────────┼────────────┼────────────┼────────────┤
245 │ON CONFLICT │ Existing & │ — │ Existing │ New row │ — │
246 │DO UPDATE │ new rows │ │ row │ │ │
247 ├─────────────┴─────────────┴────────────┴────────────┴────────────┴────────────┤
248 │---- │
249 │[a] If read access is required to the existing or new row (for │
250 │example, a WHERE or RETURNING clause that refers to columns from │
251 │the relation). │
252 └───────────────────────────────────────────────────────────────────────────────┘
253
254 Application of Multiple Policies
255 When multiple policies of different command types apply to the same
256 command (for example, SELECT and UPDATE policies applied to an UPDATE
257 command), then the user must have both types of permissions (for
258 example, permission to select rows from the relation as well as
259 permission to update them). Thus the expressions for one type of policy
260 are combined with the expressions for the other type of policy using
261 the AND operator.
262
263 When multiple policies of the same command type apply to the same
264 command, then there must be at least one PERMISSIVE policy granting
265 access to the relation, and all of the RESTRICTIVE policies must pass.
266 Thus all the PERMISSIVE policy expressions are combined using OR, all
267 the RESTRICTIVE policy expressions are combined using AND, and the
268 results are combined using AND. If there are no PERMISSIVE policies,
269 then access is denied.
270
271 Note that, for the purposes of combining multiple policies, ALL
272 policies are treated as having the same type as whichever other type of
273 policy is being applied.
274
275 For example, in an UPDATE command requiring both SELECT and UPDATE
276 permissions, if there are multiple applicable policies of each type,
277 they will be combined as follows:
278
279 expression from RESTRICTIVE SELECT/ALL policy 1
280 AND
281 expression from RESTRICTIVE SELECT/ALL policy 2
282 AND
283 ...
284 AND
285 (
286 expression from PERMISSIVE SELECT/ALL policy 1
287 OR
288 expression from PERMISSIVE SELECT/ALL policy 2
289 OR
290 ...
291 )
292 AND
293 expression from RESTRICTIVE UPDATE/ALL policy 1
294 AND
295 expression from RESTRICTIVE UPDATE/ALL policy 2
296 AND
297 ...
298 AND
299 (
300 expression from PERMISSIVE UPDATE/ALL policy 1
301 OR
302 expression from PERMISSIVE UPDATE/ALL policy 2
303 OR
304 ...
305 )
306
308 You must be the owner of a table to create or change policies for it.
309
310 While policies will be applied for explicit queries against tables in
311 the database, they are not applied when the system is performing
312 internal referential integrity checks or validating constraints. This
313 means there are indirect ways to determine that a given value exists.
314 An example of this is attempting to insert a duplicate value into a
315 column that is a primary key or has a unique constraint. If the insert
316 fails then the user can infer that the value already exists. (This
317 example assumes that the user is permitted by policy to insert records
318 which they are not allowed to see.) Another example is where a user is
319 allowed to insert into a table which references another, otherwise
320 hidden table. Existence can be determined by the user inserting values
321 into the referencing table, where success would indicate that the value
322 exists in the referenced table. These issues can be addressed by
323 carefully crafting policies to prevent users from being able to insert,
324 delete, or update records at all which might possibly indicate a value
325 they are not otherwise able to see, or by using generated values (e.g.,
326 surrogate keys) instead of keys with external meanings.
327
328 Generally, the system will enforce filter conditions imposed using
329 security policies prior to qualifications that appear in user queries,
330 in order to prevent inadvertent exposure of the protected data to
331 user-defined functions which might not be trustworthy. However,
332 functions and operators marked by the system (or the system
333 administrator) as LEAKPROOF may be evaluated before policy expressions,
334 as they are assumed to be trustworthy.
335
336 Since policy expressions are added to the user's query directly, they
337 will be run with the rights of the user running the overall query.
338 Therefore, users who are using a given policy must be able to access
339 any tables or functions referenced in the expression or they will
340 simply receive a permission denied error when attempting to query the
341 table that has row-level security enabled. This does not change how
342 views work, however. As with normal queries and views, permission
343 checks and policies for the tables which are referenced by a view will
344 use the view owner's rights and any policies which apply to the view
345 owner.
346
347 Additional discussion and practical examples can be found in
348 Section 5.8.
349
351 CREATE POLICY is a PostgreSQL extension.
352
354 ALTER POLICY (ALTER_POLICY(7)), DROP POLICY (DROP_POLICY(7)), ALTER
355 TABLE (ALTER_TABLE(7))
356
357
358
359PostgreSQL 14.3 2022 CREATE POLICY(7)