]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_POLICY.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_POLICY.7
1 '\" t
2 .\"     Title: CREATE POLICY
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE POLICY" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_POLICY \- define a new row\-level security policy for a table
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE POLICY \fIname\fR ON \fItable_name\fR
36     [ AS { PERMISSIVE | RESTRICTIVE } ]
37     [ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
38     [ TO { \fIrole_name\fR | PUBLIC | CURRENT_ROLE | CURRENT_USER | SESSION_USER } [, \&.\&.\&.] ]
39     [ USING ( \fIusing_expression\fR ) ]
40     [ WITH CHECK ( \fIcheck_expression\fR ) ]
41 .fi
42 .SH "DESCRIPTION"
43 .PP
44 The
45 \fBCREATE POLICY\fR
46 command defines a new row\-level security policy for a table\&. Note that row\-level security must be enabled on the table (using
47 \fBALTER TABLE \&.\&.\&. ENABLE ROW LEVEL SECURITY\fR) in order for created policies to be applied\&.
48 .PP
49 A policy grants the permission to select, insert, update, or delete rows that match the relevant policy expression\&. Existing table rows are checked against the expression specified in
50 USING, while new rows that would be created via
51 INSERT
52 or
53 UPDATE
54 are checked against the expression specified in
55 WITH CHECK\&. When a
56 USING
57 expression returns true for a given row then that row is visible to the user, while if false or null is returned then the row is not visible\&. When a
58 WITH CHECK
59 expression returns true for a row then that row is inserted or updated, while if false or null is returned then an error occurs\&.
60 .PP
61 For
62 \fBINSERT\fR,
63 \fBUPDATE\fR, and
64 \fBMERGE\fR
65 statements,
66 WITH CHECK
67 expressions are enforced after
68 BEFORE
69 triggers are fired, and before any actual data modifications are made\&. Thus a
70 BEFORE ROW
71 trigger may modify the data to be inserted, affecting the result of the security policy check\&.
72 WITH CHECK
73 expressions are enforced before any other constraints\&.
74 .PP
75 Policy names are per\-table\&. Therefore, one policy name can be used for many different tables and have a definition for each table which is appropriate to that table\&.
76 .PP
77 Policies can be applied for specific commands or for specific roles\&. The default for newly created policies is that they apply for all commands and roles, unless otherwise specified\&. Multiple policies may apply to a single command; see below for more details\&.
78 Table\ \&300
79 summarizes how the different types of policy apply to specific commands\&.
80 .PP
81 For policies that can have both
82 USING
83 and
84 WITH CHECK
85 expressions (ALL
86 and
87 UPDATE), if no
88 WITH CHECK
89 expression is defined, then the
90 USING
91 expression will be used both to determine which rows are visible (normal
92 USING
93 case) and which new rows will be allowed to be added (WITH CHECK
94 case)\&.
95 .PP
96 If row\-level security is enabled for a table, but no applicable policies exist, a
97 \(lqdefault deny\(rq
98 policy is assumed, so that no rows will be visible or updatable\&.
99 .SH "PARAMETERS"
100 .PP
101 \fIname\fR
102 .RS 4
103 The name of the policy to be created\&. This must be distinct from the name of any other policy for the table\&.
104 .RE
105 .PP
106 \fItable_name\fR
107 .RS 4
108 The name (optionally schema\-qualified) of the table the policy applies to\&.
109 .RE
110 .PP
111 PERMISSIVE
112 .RS 4
113 Specify that the policy is to be created as a permissive policy\&. All permissive policies which are applicable to a given query will be combined together using the Boolean
114 \(lqOR\(rq
115 operator\&. By creating permissive policies, administrators can add to the set of records which can be accessed\&. Policies are permissive by default\&.
116 .RE
117 .PP
118 RESTRICTIVE
119 .RS 4
120 Specify that the policy is to be created as a restrictive policy\&. All restrictive policies which are applicable to a given query will be combined together using the Boolean
121 \(lqAND\(rq
122 operator\&. By creating restrictive policies, administrators can reduce the set of records which can be accessed as all restrictive policies must be passed for each record\&.
123 .sp
124 Note that there needs to be at least one permissive policy to grant access to records before restrictive policies can be usefully used to reduce that access\&. If only restrictive policies exist, then no records will be accessible\&. When a mix of permissive and restrictive policies are present, a record is only accessible if at least one of the permissive policies passes, in addition to all the restrictive policies\&.
125 .RE
126 .PP
127 \fIcommand\fR
128 .RS 4
129 The command to which the policy applies\&. Valid options are
130 \fBALL\fR,
131 \fBSELECT\fR,
132 \fBINSERT\fR,
133 \fBUPDATE\fR, and
134 \fBDELETE\fR\&.
135 \fBALL\fR
136 is the default\&. See below for specifics regarding how these are applied\&.
137 .RE
138 .PP
139 \fIrole_name\fR
140 .RS 4
141 The role(s) to which the policy is to be applied\&. The default is
142 PUBLIC, which will apply the policy to all roles\&.
143 .RE
144 .PP
145 \fIusing_expression\fR
146 .RS 4
147 Any
148 SQL
149 conditional expression (returning
150 boolean)\&. The conditional expression cannot contain any aggregate or window functions\&. This expression will be added to queries that refer to the table if row\-level security is enabled\&. Rows for which the expression returns true will be visible\&. Any rows for which the expression returns false or null will not be visible to the user (in a
151 \fBSELECT\fR), and will not be available for modification (in an
152 \fBUPDATE\fR
153 or
154 \fBDELETE\fR)\&. Such rows are silently suppressed; no error is reported\&.
155 .RE
156 .PP
157 \fIcheck_expression\fR
158 .RS 4
159 Any
160 SQL
161 conditional expression (returning
162 boolean)\&. The conditional expression cannot contain any aggregate or window functions\&. This expression will be used in
163 \fBINSERT\fR
164 and
165 \fBUPDATE\fR
166 queries against the table if row\-level security is enabled\&. Only rows for which the expression evaluates to true will be allowed\&. An error will be thrown if the expression evaluates to false or null for any of the records inserted or any of the records that result from the update\&. Note that the
167 \fIcheck_expression\fR
168 is evaluated against the proposed new contents of the row, not the original contents\&.
169 .RE
170 .SS "Per\-Command Policies"
171 .PP
172 ALL
173 .RS 4
174 Using
175 ALL
176 for a policy means that it will apply to all commands, regardless of the type of command\&. If an
177 ALL
178 policy exists and more specific policies exist, then both the
179 ALL
180 policy and the more specific policy (or policies) will be applied\&. Additionally,
181 ALL
182 policies will be applied to both the selection side of a query and the modification side, using the
183 USING
184 expression for both cases if only a
185 USING
186 expression has been defined\&.
187 .sp
188 As an example, if an
189 UPDATE
190 is issued, then the
191 ALL
192 policy will be applicable both to what the
193 UPDATE
194 will be able to select as rows to be updated (applying the
195 USING
196 expression), and to the resulting updated rows, to check if they are permitted to be added to the table (applying the
197 WITH CHECK
198 expression, if defined, and the
199 USING
200 expression otherwise)\&. If an
201 \fBINSERT\fR
202 or
203 \fBUPDATE\fR
204 command attempts to add rows to the table that do not pass the
205 ALL
206 policy\*(Aqs
207 WITH CHECK
208 expression, the entire command will be aborted\&.
209 .RE
210 .PP
211 SELECT
212 .RS 4
213 Using
214 SELECT
215 for a policy means that it will apply to
216 SELECT
217 queries and whenever
218 SELECT
219 permissions are required on the relation the policy is defined for\&. The result is that only those records from the relation that pass the
220 SELECT
221 policy will be returned during a
222 SELECT
223 query, and that queries that require
224 SELECT
225 permissions, such as
226 UPDATE, will also only see those records that are allowed by the
227 SELECT
228 policy\&. A
229 SELECT
230 policy cannot have a
231 WITH CHECK
232 expression, as it only applies in cases where records are being retrieved from the relation\&.
233 .RE
234 .PP
235 INSERT
236 .RS 4
237 Using
238 INSERT
239 for a policy means that it will apply to
240 INSERT
241 commands and
242 MERGE
243 commands that contain
244 INSERT
245 actions\&. Rows being inserted that do not pass this policy will result in a policy violation error, and the entire
246 INSERT
247 command will be aborted\&. An
248 INSERT
249 policy cannot have a
250 USING
251 expression, as it only applies in cases where records are being added to the relation\&.
252 .sp
253 Note that
254 INSERT
255 with
256 ON CONFLICT DO UPDATE
257 checks
258 INSERT
259 policies\*(Aq
260 WITH CHECK
261 expressions only for rows appended to the relation by the
262 INSERT
263 path\&.
264 .RE
265 .PP
266 UPDATE
267 .RS 4
268 Using
269 UPDATE
270 for a policy means that it will apply to
271 UPDATE,
272 SELECT FOR UPDATE
273 and
274 SELECT FOR SHARE
275 commands, as well as auxiliary
276 ON CONFLICT DO UPDATE
277 clauses of
278 INSERT
279 commands\&.
280 MERGE
281 commands containing
282 UPDATE
283 actions are affected as well\&. Since
284 UPDATE
285 involves pulling an existing record and replacing it with a new modified record,
286 UPDATE
287 policies accept both a
288 USING
289 expression and a
290 WITH CHECK
291 expression\&. The
292 USING
293 expression determines which records the
294 UPDATE
295 command will see to operate against, while the
296 WITH CHECK
297 expression defines which modified rows are allowed to be stored back into the relation\&.
298 .sp
299 Any rows whose updated values do not pass the
300 WITH CHECK
301 expression will cause an error, and the entire command will be aborted\&. If only a
302 USING
303 clause is specified, then that clause will be used for both
304 USING
305 and
306 WITH CHECK
307 cases\&.
308 .sp
309 Typically an
310 UPDATE
311 command also needs to read data from columns in the relation being updated (e\&.g\&., in a
312 WHERE
313 clause or a
314 RETURNING
315 clause, or in an expression on the right hand side of the
316 SET
317 clause)\&. In this case,
318 SELECT
319 rights are also required on the relation being updated, and the appropriate
320 SELECT
321 or
322 ALL
323 policies will be applied in addition to the
324 UPDATE
325 policies\&. Thus the user must have access to the row(s) being updated through a
326 SELECT
327 or
328 ALL
329 policy in addition to being granted permission to update the row(s) via an
330 UPDATE
331 or
332 ALL
333 policy\&.
334 .sp
335 When an
336 INSERT
337 command has an auxiliary
338 ON CONFLICT DO UPDATE
339 clause, if the
340 UPDATE
341 path is taken, the row to be updated is first checked against the
342 USING
343 expressions of any
344 UPDATE
345 policies, and then the new updated row is checked against the
346 WITH CHECK
347 expressions\&. Note, however, that unlike a standalone
348 UPDATE
349 command, if the existing row does not pass the
350 USING
351 expressions, an error will be thrown (the
352 UPDATE
353 path will
354 \fInever\fR
355 be silently avoided)\&.
356 .RE
357 .PP
358 DELETE
359 .RS 4
360 Using
361 DELETE
362 for a policy means that it will apply to
363 DELETE
364 commands\&. Only rows that pass this policy will be seen by a
365 DELETE
366 command\&. There can be rows that are visible through a
367 SELECT
368 that are not available for deletion, if they do not pass the
369 USING
370 expression for the
371 DELETE
372 policy\&.
373 .sp
374 In most cases a
375 DELETE
376 command also needs to read data from columns in the relation that it is deleting from (e\&.g\&., in a
377 WHERE
378 clause or a
379 RETURNING
380 clause)\&. In this case,
381 SELECT
382 rights are also required on the relation, and the appropriate
383 SELECT
384 or
385 ALL
386 policies will be applied in addition to the
387 DELETE
388 policies\&. Thus the user must have access to the row(s) being deleted through a
389 SELECT
390 or
391 ALL
392 policy in addition to being granted permission to delete the row(s) via a
393 DELETE
394 or
395 ALL
396 policy\&.
397 .sp
398 A
399 DELETE
400 policy cannot have a
401 WITH CHECK
402 expression, as it only applies in cases where records are being deleted from the relation, so that there is no new row to check\&.
403 .RE
404 .sp
405 .it 1 an-trap
406 .nr an-no-space-flag 1
407 .nr an-break-flag 1
408 .br
409 .B Table\ \&300.\ \&Policies Applied by Command Type
410 .TS
411 allbox tab(:);
412 lB lB lB lB s lB
413 ^ lB lB lB lB lB.
414 T{
415 Command
416 T}:T{
417 SELECT/ALL policy
418 T}:T{
419 INSERT/ALL policy
420 T}:T{
421 UPDATE/ALL policy
422 T}:T{
423 DELETE/ALL policy
424 T}
425 :T{
426 USING expression
427 T}:T{
428 WITH CHECK expression
429 T}:T{
430 USING expression
431 T}:T{
432 WITH CHECK expression
433 T}:T{
434 USING expression
435 T}
436 .T&
437 l l l l l l
438 l l l l l l
439 l l l l l l
440 l l l l l l
441 l l l l l l
442 l l l l l l
443 l l l l l l
444 l s s s s s.
445 T{
446 \fBSELECT\fR
447 T}:T{
448 Existing row
449 T}:T{
450 \(em
451 T}:T{
452 \(em
453 T}:T{
454 \(em
455 T}:T{
456 \(em
457 T}
458 T{
459 \fBSELECT FOR UPDATE/SHARE\fR
460 T}:T{
461 Existing row
462 T}:T{
463 \(em
464 T}:T{
465 Existing row
466 T}:T{
467 \(em
468 T}:T{
469 \(em
470 T}
471 T{
472 \fBINSERT\fR / \fBMERGE \&.\&.\&. THEN INSERT\fR
473 T}:T{
474 \(em
475 T}:T{
476 New row
477 T}:T{
478 \(em
479 T}:T{
480 \(em
481 T}:T{
482 \(em
483 T}
484 T{
485 \fBINSERT \&.\&.\&. RETURNING\fR
486 T}:T{
487 New row [a]
488 T}:T{
489 New row
490 T}:T{
491 \(em
492 T}:T{
493 \(em
494 T}:T{
495 \(em
496 T}
497 T{
498 \fBUPDATE\fR / \fBMERGE \&.\&.\&. THEN UPDATE\fR
499 T}:T{
500 Existing & new rows [a]
501 T}:T{
502 \(em
503 T}:T{
504 Existing row
505 T}:T{
506 New row
507 T}:T{
508 \(em
509 T}
510 T{
511 \fBDELETE\fR
512 T}:T{
513 Existing row [a]
514 T}:T{
515 \(em
516 T}:T{
517 \(em
518 T}:T{
519 \(em
520 T}:T{
521 Existing row
522 T}
523 T{
524 \fBON CONFLICT DO UPDATE\fR
525 T}:T{
526 Existing & new rows
527 T}:T{
528 \(em
529 T}:T{
530 Existing row
531 T}:T{
532 New row
533 T}:T{
534 \(em
535 T}
536 T{
537 ----
538 .br
539 [a]
540 If read access is required to the existing or new row (for example, a
541 WHERE
542 or
543 RETURNING
544 clause that refers to columns from the relation)\&.
545 T}
546 .TE
547 .sp 1
548 .SS "Application of Multiple Policies"
549 .PP
550 When multiple policies of different command types apply to the same command (for example,
551 SELECT
552 and
553 UPDATE
554 policies applied to an
555 UPDATE
556 command), then the user must have both types of permissions (for example, permission to select rows from the relation as well as permission to update them)\&. Thus the expressions for one type of policy are combined with the expressions for the other type of policy using the
557 AND
558 operator\&.
559 .PP
560 When multiple policies of the same command type apply to the same command, then there must be at least one
561 PERMISSIVE
562 policy granting access to the relation, and all of the
563 RESTRICTIVE
564 policies must pass\&. Thus all the
565 PERMISSIVE
566 policy expressions are combined using
567 OR, all the
568 RESTRICTIVE
569 policy expressions are combined using
570 AND, and the results are combined using
571 AND\&. If there are no
572 PERMISSIVE
573 policies, then access is denied\&.
574 .PP
575 Note that, for the purposes of combining multiple policies,
576 ALL
577 policies are treated as having the same type as whichever other type of policy is being applied\&.
578 .PP
579 For example, in an
580 UPDATE
581 command requiring both
582 SELECT
583 and
584 UPDATE
585 permissions, if there are multiple applicable policies of each type, they will be combined as follows:
586 .sp
587 .if n \{\
588 .RS 4
589 .\}
590 .nf
591 \fIexpression\fR from RESTRICTIVE SELECT/ALL policy 1
592 AND
593 \fIexpression\fR from RESTRICTIVE SELECT/ALL policy 2
594 AND
595 \&.\&.\&.
596 AND
597 (
598   \fIexpression\fR from PERMISSIVE SELECT/ALL policy 1
599   OR
600   \fIexpression\fR from PERMISSIVE SELECT/ALL policy 2
601   OR
602   \&.\&.\&.
603 )
604 AND
605 \fIexpression\fR from RESTRICTIVE UPDATE/ALL policy 1
606 AND
607 \fIexpression\fR from RESTRICTIVE UPDATE/ALL policy 2
608 AND
609 \&.\&.\&.
610 AND
611 (
612   \fIexpression\fR from PERMISSIVE UPDATE/ALL policy 1
613   OR
614   \fIexpression\fR from PERMISSIVE UPDATE/ALL policy 2
615   OR
616   \&.\&.\&.
617 )
618 .fi
619 .if n \{\
620 .RE
621 .\}
622 .SH "NOTES"
623 .PP
624 You must be the owner of a table to create or change policies for it\&.
625 .PP
626 While policies will be applied for explicit queries against tables in the database, they are not applied when the system is performing internal referential integrity checks or validating constraints\&. This means there are indirect ways to determine that a given value exists\&. An example of this is attempting to insert a duplicate value into a column that is a primary key or has a unique constraint\&. If the insert fails then the user can infer that the value already exists\&. (This example assumes that the user is permitted by policy to insert records which they are not allowed to see\&.) Another example is where a user is allowed to insert into a table which references another, otherwise hidden table\&. Existence can be determined by the user inserting values into the referencing table, where success would indicate that the value exists in the referenced table\&. These issues can be addressed by carefully crafting policies to prevent users from being able to insert, delete, or update records at all which might possibly indicate a value they are not otherwise able to see, or by using generated values (e\&.g\&., surrogate keys) instead of keys with external meanings\&.
627 .PP
628 Generally, the system will enforce filter conditions imposed using security policies prior to qualifications that appear in user queries, in order to prevent inadvertent exposure of the protected data to user\-defined functions which might not be trustworthy\&. However, functions and operators marked by the system (or the system administrator) as
629 LEAKPROOF
630 may be evaluated before policy expressions, as they are assumed to be trustworthy\&.
631 .PP
632 Since policy expressions are added to the user\*(Aqs query directly, they will be run with the rights of the user running the overall query\&. Therefore, users who are using a given policy must be able to access any tables or functions referenced in the expression or they will simply receive a permission denied error when attempting to query the table that has row\-level security enabled\&. This does not change how views work, however\&. As with normal queries and views, permission checks and policies for the tables which are referenced by a view will use the view owner\*(Aqs rights and any policies which apply to the view owner, except if the view is defined using the
633 security_invoker
634 option (see
635 \fBCREATE VIEW\fR)\&.
636 .PP
637 No separate policy exists for
638 \fBMERGE\fR\&. Instead, the policies defined for
639 \fBSELECT\fR,
640 \fBINSERT\fR,
641 \fBUPDATE\fR, and
642 \fBDELETE\fR
643 are applied while executing
644 \fBMERGE\fR, depending on the actions that are performed\&.
645 .PP
646 Additional discussion and practical examples can be found in
647 Section\ \&5.9\&.
648 .SH "COMPATIBILITY"
649 .PP
650 \fBCREATE POLICY\fR
651 is a
652 PostgreSQL
653 extension\&.
654 .SH "SEE ALSO"
655 ALTER POLICY (\fBALTER_POLICY\fR(7)), DROP POLICY (\fBDROP_POLICY\fR(7)), ALTER TABLE (\fBALTER_TABLE\fR(7))