2 .\" Title: CREATE POLICY
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_POLICY \- define a new row\-level security policy for a table
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 ) ]
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\&.
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
54 are checked against the expression specified in
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
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\&.
67 expressions are enforced after
69 triggers are fired, and before any actual data modifications are made\&. Thus a
71 trigger may modify the data to be inserted, affecting the result of the security policy check\&.
73 expressions are enforced before any other constraints\&.
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\&.
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\&.
79 summarizes how the different types of policy apply to specific commands\&.
81 For policies that can have both
89 expression is defined, then the
91 expression will be used both to determine which rows are visible (normal
93 case) and which new rows will be allowed to be added (WITH CHECK
96 If row\-level security is enabled for a table, but no applicable policies exist, a
98 policy is assumed, so that no rows will be visible or updatable\&.
103 The name of the policy to be created\&. This must be distinct from the name of any other policy for the table\&.
108 The name (optionally schema\-qualified) of the table the policy applies to\&.
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
115 operator\&. By creating permissive policies, administrators can add to the set of records which can be accessed\&. Policies are permissive by default\&.
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
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\&.
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\&.
129 The command to which the policy applies\&. Valid options are
136 is the default\&. See below for specifics regarding how these are applied\&.
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\&.
145 \fIusing_expression\fR
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
154 \fBDELETE\fR)\&. Such rows are silently suppressed; no error is reported\&.
157 \fIcheck_expression\fR
161 conditional expression (returning
162 boolean)\&. The conditional expression cannot contain any aggregate or window functions\&. This expression will be used in
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\&.
170 .SS "Per\-Command Policies"
176 for a policy means that it will apply to all commands, regardless of the type of command\&. If an
178 policy exists and more specific policies exist, then both the
180 policy and the more specific policy (or policies) will be applied\&. Additionally,
182 policies will be applied to both the selection side of a query and the modification side, using the
184 expression for both cases if only a
186 expression has been defined\&.
192 policy will be applicable both to what the
194 will be able to select as rows to be updated (applying the
196 expression), and to the resulting updated rows, to check if they are permitted to be added to the table (applying the
198 expression, if defined, and the
200 expression otherwise)\&. If an
204 command attempts to add rows to the table that do not pass the
208 expression, the entire command will be aborted\&.
215 for a policy means that it will apply to
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
221 policy will be returned during a
223 query, and that queries that require
226 UPDATE, will also only see those records that are allowed by the
232 expression, as it only applies in cases where records are being retrieved from the relation\&.
239 for a policy means that it will apply to
243 commands that contain
245 actions\&. Rows being inserted that do not pass this policy will result in a policy violation error, and the entire
247 command will be aborted\&. An
251 expression, as it only applies in cases where records are being added to the relation\&.
256 ON CONFLICT DO UPDATE
261 expressions only for rows appended to the relation by the
270 for a policy means that it will apply to
275 commands, as well as auxiliary
276 ON CONFLICT DO UPDATE
283 actions are affected as well\&. Since
285 involves pulling an existing record and replacing it with a new modified record,
287 policies accept both a
293 expression determines which records the
295 command will see to operate against, while the
297 expression defines which modified rows are allowed to be stored back into the relation\&.
299 Any rows whose updated values do not pass the
301 expression will cause an error, and the entire command will be aborted\&. If only a
303 clause is specified, then that clause will be used for both
311 command also needs to read data from columns in the relation being updated (e\&.g\&., in a
315 clause, or in an expression on the right hand side of the
317 clause)\&. In this case,
319 rights are also required on the relation being updated, and the appropriate
323 policies will be applied in addition to the
325 policies\&. Thus the user must have access to the row(s) being updated through a
329 policy in addition to being granted permission to update the row(s) via an
337 command has an auxiliary
338 ON CONFLICT DO UPDATE
341 path is taken, the row to be updated is first checked against the
345 policies, and then the new updated row is checked against the
347 expressions\&. Note, however, that unlike a standalone
349 command, if the existing row does not pass the
351 expressions, an error will be thrown (the
355 be silently avoided)\&.
362 for a policy means that it will apply to
364 commands\&. Only rows that pass this policy will be seen by a
366 command\&. There can be rows that are visible through a
368 that are not available for deletion, if they do not pass the
376 command also needs to read data from columns in the relation that it is deleting from (e\&.g\&., in a
380 clause)\&. In this case,
382 rights are also required on the relation, and the appropriate
386 policies will be applied in addition to the
388 policies\&. Thus the user must have access to the row(s) being deleted through a
392 policy in addition to being granted permission to delete the row(s) via a
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\&.
406 .nr an-no-space-flag 1
409 .B Table\ \&300.\ \&Policies Applied by Command Type
428 WITH CHECK expression
432 WITH CHECK expression
459 \fBSELECT FOR UPDATE/SHARE\fR
472 \fBINSERT\fR / \fBMERGE \&.\&.\&. THEN INSERT\fR
485 \fBINSERT \&.\&.\&. RETURNING\fR
498 \fBUPDATE\fR / \fBMERGE \&.\&.\&. THEN UPDATE\fR
500 Existing & new rows [a]
524 \fBON CONFLICT DO UPDATE\fR
540 If read access is required to the existing or new row (for example, a
544 clause that refers to columns from the relation)\&.
548 .SS "Application of Multiple Policies"
550 When multiple policies of different command types apply to the same command (for example,
554 policies applied to an
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
560 When multiple policies of the same command type apply to the same command, then there must be at least one
562 policy granting access to the relation, and all of the
564 policies must pass\&. Thus all the
566 policy expressions are combined using
569 policy expressions are combined using
570 AND, and the results are combined using
571 AND\&. If there are no
573 policies, then access is denied\&.
575 Note that, for the purposes of combining multiple policies,
577 policies are treated as having the same type as whichever other type of policy is being applied\&.
581 command requiring both
585 permissions, if there are multiple applicable policies of each type, they will be combined as follows:
591 \fIexpression\fR from RESTRICTIVE SELECT/ALL policy 1
593 \fIexpression\fR from RESTRICTIVE SELECT/ALL policy 2
598 \fIexpression\fR from PERMISSIVE SELECT/ALL policy 1
600 \fIexpression\fR from PERMISSIVE SELECT/ALL policy 2
605 \fIexpression\fR from RESTRICTIVE UPDATE/ALL policy 1
607 \fIexpression\fR from RESTRICTIVE UPDATE/ALL policy 2
612 \fIexpression\fR from PERMISSIVE UPDATE/ALL policy 1
614 \fIexpression\fR from PERMISSIVE UPDATE/ALL policy 2
624 You must be the owner of a table to create or change policies for it\&.
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\&.
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
630 may be evaluated before policy expressions, as they are assumed to be trustworthy\&.
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
635 \fBCREATE VIEW\fR)\&.
637 No separate policy exists for
638 \fBMERGE\fR\&. Instead, the policies defined for
643 are applied while executing
644 \fBMERGE\fR, depending on the actions that are performed\&.
646 Additional discussion and practical examples can be found in
655 ALTER POLICY (\fBALTER_POLICY\fR(7)), DROP POLICY (\fBDROP_POLICY\fR(7)), ALTER TABLE (\fBALTER_TABLE\fR(7))