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