CREATE_POLICY(7pg14)
π CREATE_POLICY(7) π
β¨ NAME β¨
Okay bestie, this is the CREATE_POLICY(7) page from the PostgreSQL 14.24 docs, and it's literally about how to define a new row-level security policy for a table. Security glow-up, no cap. π«Ά
π«Ά SYNOPSIS π«Ά
CREATE POLICY name ON table_name
[ AS { PERMISSIVE | RESTRICTIVE } ]
[ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
[ TO { role_name | PUBLIC | CURRENT_ROLE | CURRENT_USER | SESSION_USER } [, ...] ]
[ USING ( using_expression ) ]
[ WITH CHECK ( check_expression ) ]
π DESCRIPTION π
So the CREATE POLICY command defines a shiny new row-level security policy for a table. But heads up: row-level security has to be enabled on the table first β you do that with ALTER TABLE ... ENABLE ROW LEVEL SECURITY β otherwise the policies you create just sit there and don't get applied. Important detail, bestie. β¨
A policy grants permission to select, insert, update, or delete rows that match the relevant policy expression. Existing table rows are checked against the expression in USING, while new rows that would be created via INSERT or UPDATE are checked against the expression in WITH CHECK. When a USING expression returns true for a given row, that row is visible to the user; if it returns false or null, the row is not visible. Typically no error occurs when a row is not visible, but see Table 281 for exceptions. When a WITH CHECK expression returns true for a row, that row is inserted or updated; if it returns false or null, an error occurs. π
For INSERT and UPDATE statements, WITH CHECK expressions are enforced after BEFORE triggers are fired, and before any actual data modifications are made. So a BEFORE ROW trigger may modify the data to be inserted, affecting the result of the security policy check. WITH CHECK expressions are enforced before any other constraints. Order matters, iykyk. π₯
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. Reusable name, table-specific vibe. π
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. Table 281 summarizes how the different types of policy apply to specific commands. π
For policies that can have both USING and WITH CHECK expressions (ALL and UPDATE), if no WITH CHECK expression is defined, then the USING expression will be used both to determine which rows are visible (normal USING case) and which new rows will be allowed to be added (WITH CHECK case). Two jobs, one expression. π
If row-level security is enabled for a table, but no applicable policies exist, a βdefault denyβ policy is assumed, so that no rows will be visible or updatable. No policy, no access. Periodt. π
π PARAMETERS π
nameβ the name of the policy to be created. This must be distinct from the name of any other policy for the table. No duplicate names for the same table, bestie.table_nameβ the name (optionally schema-qualified) of the table the policy applies to.PERMISSIVEβ says 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 βORβ operator. By creating permissive policies, administrators can add to the set of records which can be accessed. Policies are permissive by default. OR energy: add access.RESTRICTIVEβ says 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 βANDβ 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. AND energy: tighten access.
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. One permissive pass plus every restrictive pass, or it's a no. π«Ά
commandβ the command to which the policy applies. Valid options areALL,SELECT,INSERT,UPDATE, andDELETE.ALLis the default. See below for specifics regarding how these are applied.role_nameβ the role(s) to which the policy is to be applied. The default isPUBLIC, which will apply the policy to all roles.using_expressionβ any SQL conditional expression (returning 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 aSELECT), and will not be available for modification (in anUPDATEorDELETE). Typically, such rows are silently suppressed; no error is reported (but see Table 281 for exceptions). Visible or invisible β that's theUSINGvibe check.check_expressionβ any SQL conditional expression (returning boolean). The conditional expression cannot contain any aggregate or window functions. This expression will be used inINSERTandUPDATEqueries 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 thecheck_expressionis evaluated against the proposed new contents of the row, not the original contents. New row energy only β not the original row. π
π Per-Command Policies π
π₯ ALL π₯
Using ALL for a policy means that it will apply to all commands, regardless of the type of command. If an ALL policy exists and more specific policies exist, then both the ALL policy and the more specific policy (or policies) will be applied. Additionally, ALL policies will be applied to both the selection side of a query and the modification side, using the USING expression for both cases if only a USING expression has been defined. Main character policy: applies everywhere. π
As an example, if an UPDATE is issued, then the ALL policy will be applicable both to what the UPDATE will be able to select as rows to be updated (applying the USING expression), and to the resulting updated rows, to check if they are permitted to be added to the table (applying the WITH CHECK expression, if defined, and the USING expression otherwise). If an INSERT or UPDATE command attempts to add rows to the table that do not pass the ALL policy's WITH CHECK expression (or its USING expression, if it does not have a WITH CHECK expression), the entire command will be aborted. Whole command aborted β not just the row. Dramatic but true. π
π SELECT π
Using SELECT for a policy means that it will apply to SELECT queries and whenever SELECT permissions are required on the relation the policy is defined for. The result is that only those records from the relation that pass the SELECT policy will be returned during a SELECT query, and that queries that require SELECT permissions, such as UPDATE and DELETE, will also only see those records that are allowed by the SELECT policy. A SELECT policy cannot have a WITH CHECK expression, as it only applies in cases where records are being retrieved from the relation, except as described below. Read-only policy, so no WITH CHECK except the exceptions below. π¦
If a data-modifying query has a RETURNING clause, SELECT permissions are required on the relation, and any newly inserted or updated rows from the relation must satisfy the relation's SELECT policies in order to be available to the RETURNING clause. If a newly inserted or updated row does not satisfy the relation's SELECT policies, an error will be thrown (inserted or updated rows to be returned are never silently ignored). Never silently ignored, bestie. π
If an INSERT has an ON CONFLICT DO UPDATE clause, or an ON CONFLICT DO NOTHING clause with an arbiter index or constraint specification, then SELECT permissions are required on the relation, and the rows proposed for insertion are checked using the relation's SELECT policies. If a row proposed for insertion does not satisfy the relation's SELECT policies, an error is thrown (the INSERT is never silently avoided). In addition, if the UPDATE path is taken, the row to be updated and the new updated row are checked against the relation's SELECT policies, and an error is thrown if they are not satisfied (an auxiliary UPDATE is never silently avoided). No sneaky silent avoids. Not today. β¨
π¦ INSERT π¦
Using INSERT for a policy means that it will apply to INSERT commands. Rows being inserted that do not pass this policy will result in a policy violation error, and the entire INSERT command will be aborted. An INSERT policy cannot have a USING expression, as it only applies in cases where records are being added to the relation. Adding rows only β so no USING. π
Note that an INSERT with an ON CONFLICT DO NOTHING/UPDATE clause will check the INSERT policies' WITH CHECK expressions for all rows proposed for insertion, regardless of whether or not they end up being inserted. Proposed rows get checked even if they don't make it in. π
π UPDATE π
Using UPDATE for a policy means that it will apply to UPDATE, SELECT FOR UPDATE and SELECT FOR SHARE commands, as well as auxiliary ON CONFLICT DO UPDATE clauses of INSERT commands. Since UPDATE involves pulling an existing record and replacing it with a new modified record, UPDATE policies accept both a USING expression and a WITH CHECK expression. The USING expression determines which records the UPDATE command will see to operate against, while the WITH CHECK expression defines which modified rows are allowed to be stored back into the relation. USING picks the rows, WITH CHECK approves the glow-up. π₯
Any rows whose updated values do not pass the WITH CHECK expression will cause an error, and the entire command will be aborted. If only a USING clause is specified, then that clause will be used for both USING and WITH CHECK cases. If the updated row fails, the whole command is done. π
Typically an UPDATE command also needs to read data from columns in the relation being updated (e.g., in a WHERE clause or a RETURNING clause, or in an expression on the right hand side of the SET clause). In this case, SELECT rights are also required on the relation being updated, and the appropriate SELECT or ALL policies will be applied in addition to the UPDATE policies. Thus the user must have access to the row(s) being updated through a SELECT or ALL policy in addition to being granted permission to update the row(s) via an UPDATE or ALL policy. Read access plus update access β both required, no shortcuts. π«Ά
When an INSERT command has an auxiliary ON CONFLICT DO UPDATE clause and the UPDATE path actually gets taken, bestie, here's the order of operations: the row that's about to be updated gets checked against the USING expressions of any UPDATE policies first, and then the brand-new updated row gets checked against the WITH CHECK expressions. But β and this is the plot twist β unlike a standalone UPDATE command, if the existing row doesn't pass the USING expressions, an error gets thrown. The UPDATE path will never be silently avoided. No sneaky sidestep, no quiet exit. It just errors. π
DELETE
Using DELETE for a policy means it applies to DELETE commands, periodt. Only rows that pass this policy will be seen by a DELETE command. And yes, there can absolutely be rows that are visible through a SELECT policy but not available for deletion β that happens when they don't pass the USING expression for the DELETE policy. Awkward but true. π
In most cases a DELETE command also needs to read data from columns in the relation it's deleting from (think: a WHERE clause, or a RETURNING clause). When that happens, SELECT rights are also required on the relation, and the appropriate SELECT or ALL policies get applied in addition to the DELETE policies. So basically the user needs access to the row(s) being deleted through a SELECT or ALL policy and needs permission to delete those row(s) via a DELETE or ALL policy. Both. Not one or the other. π
A DELETE policy cannot have a WITH CHECK expression β it only applies in cases where records are being deleted from the relation, so there's literally no new row to check. Nothing to look at. Empty chair. πͺ
Table 281 summarizes how the different types of policy apply to specific commands. In the table, βcheckβ means the policy expression is checked and an error is thrown if it returns false or null, whereas βfilterβ means the row is silently ignored if the policy expression returns false or null.
Table 281. Policies Applied by Command Type
SELECT / COPY ... TO
SELECT/ALLpolicy,USINGexpression: Filter existing rowINSERT/ALLpolicy,WITH CHECKexpression: βUPDATE/ALLpolicy,USINGexpression: βUPDATE/ALLpolicy,WITH CHECKexpression: βDELETE/ALLpolicy,USINGexpression: β
SELECT FOR UPDATE/SHARE
SELECT/ALLpolicy,USINGexpression: Filter existing rowINSERT/ALLpolicy,WITH CHECKexpression: βUPDATE/ALLpolicy,USINGexpression: Filter existing rowUPDATE/ALLpolicy,WITH CHECKexpression: βDELETE/ALLpolicy,USINGexpression: β
INSERT
SELECT/ALLpolicy,USINGexpression: Check new row [a]INSERT/ALLpolicy,WITH CHECKexpression: Check new rowUPDATE/ALLpolicy,USINGexpression: βUPDATE/ALLpolicy,WITH CHECKexpression: βDELETE/ALLpolicy,USINGexpression: β
UPDATE
SELECT/ALLpolicy,USINGexpression: Filter existing row [a] & check new row [a]INSERT/ALLpolicy,WITH CHECKexpression: βUPDATE/ALLpolicy,USINGexpression: Filter existing rowUPDATE/ALLpolicy,WITH CHECKexpression: Check new rowDELETE/ALLpolicy,USINGexpression: β
DELETE
SELECT/ALLpolicy,USINGexpression: Filter existing row [a]INSERT/ALLpolicy,WITH CHECKexpression: βUPDATE/ALLpolicy,USINGexpression: βUPDATE/ALLpolicy,WITH CHECKexpression: βDELETE/ALLpolicy,USINGexpression: Filter existing row
INSERT ... ON CONFLICT
SELECT/ALLpolicy,USINGexpression: Check new row [b][c]INSERT/ALLpolicy,WITH CHECKexpression: Check new row [c]UPDATE/ALLpolicy,USINGexpression: βUPDATE/ALLpolicy,WITH CHECKexpression: βDELETE/ALLpolicy,USINGexpression: β
ON CONFLICT DO UPDATE
SELECT/ALLpolicy,USINGexpression: Check existing & new rows [d]INSERT/ALLpolicy,WITH CHECKexpression: βUPDATE/ALLpolicy,USINGexpression: Check existing rowUPDATE/ALLpolicy,WITH CHECKexpression: Check new row [d]DELETE/ALLpolicy,USINGexpression: β
πΈ The footnotes, iconically:[a]If read access is required to either the existing or new row (for example, aWHEREorRETURNINGclause that refers to columns from the relation).[b]If an arbiter index or constraint is specified.[c]Row proposed for insertion is checked regardless of whether or not a conflict occurs.[d]New row of the auxiliaryUPDATEcommand, which might be different from the new row of the originalINSERTcommand.
Application of Multiple Policies
Okay so when multiple policies of different command types apply to the same command β like SELECT and UPDATE policies both landing on an UPDATE command β the user has to have both types of permissions. Both. (For example: permission to select rows from the relation and permission to update them.) So the expressions for one type of policy get combined with the expressions for the other type using the AND operator. Package deal, no splitting. π«Ά
When multiple policies of the same command type apply to the same command, then there must be at least one PERMISSIVE policy granting access to the relation, and all of the RESTRICTIVE policies must pass. So: all the PERMISSIVE policy expressions get combined using OR, all the RESTRICTIVE policy expressions get combined using AND, and then those results get combined using AND. And if there are no PERMISSIVE policies at all? Access is denied. Straight up. π«
Heads up: for the purposes of combining multiple policies, ALL policies are treated as having the same type as whichever other type of policy is being applied. Shape-shifter energy. β¨
For example, in an UPDATE command requiring both SELECT and UPDATE permissions, if there are multiple applicable policies of each type, they'll be combined like this:
expression from RESTRICTIVE SELECT/ALL policy 1
AND
expression from RESTRICTIVE SELECT/ALL policy 2
AND
...
AND
(
expression from PERMISSIVE SELECT/ALL policy 1
OR
expression from PERMISSIVE SELECT/ALL policy 2
OR
...
)
AND
expression from RESTRICTIVE UPDATE/ALL policy 1
AND
expression from RESTRICTIVE UPDATE/ALL policy 2
AND
...
AND
(
expression from PERMISSIVE UPDATE/ALL policy 1
OR
expression from PERMISSIVE UPDATE/ALL policy 2
OR
...
)
Lowkey beautiful once you see the shape of it β restrictive walls first, then a big friendly OR-pile of permissive options, all held together with ANDs. π
πΈ NOTES πΈ
Okay bestie, gather round, because this part is lowkey the most tea-spilling section of the whole page π«β¨
- You must be the owner of a table to create or change policies for it. No owner energy, no policy power π
So here's the vibe check on how policies actually behave, and ngl it's a little spicy πΆοΈ
While policies are applied for explicit queries against tables in the database, they are not applied when the system is doing its internal referential integrity checks or validating constraints. Translation: there are indirect ways to figure out that a given value exists π. The docs give the receipts:
- If you try to insert a duplicate value into a column that's a primary key or has a unique constraint, and the insert fails? Boom β the user can now infer that the value already exists. π (This example assumes the user is permitted by policy to insert records that they're not allowed to see, iykyk.)
- Another one: a user is allowed to insert into a table that references another, otherwise hidden table. They can determine existence by inserting values into the referencing table β if it succeeds, that indicates the value exists in the referenced table. Sneaky, sneaky π΅οΈββοΈ
How to fix it: carefully craft your policies so users literally can't insert, delete, or update records at all in ways that might hint at a value they can't otherwise see π« β or use generated values (e.g., surrogate keys) instead of keys with external meanings. Slay π
Generally, the system enforces filter conditions imposed using security policies prior to qualifications that appear in user queries, so protected data doesn't get accidentally exposed to user-defined functions that might not be trustworthy π‘οΈ. However β and this is the important bit β functions and operators marked by the system (or the system administrator) as LEAKPROOF may be evaluated before policy expressions, because they're assumed to be trustworthy. Trust the process, bestie π«Ά
Another thing you need to know: since policy expressions are added to the user's query directly, they run with the rights of the user running the overall query. So everybody using a given policy must be able to access any tables or functions referenced in the expression β otherwise they'll just get a permission denied error when they try to query the table that has row-level security enabled π. That's the whole vibe.
But this doesn't change how views work, okay? As with normal queries and views, permission checks and policies for the tables referenced by a view will use the view owner's rights and any policies that apply to the view owner. Main character energy: the view owner ππ
And if you want the full deep dive plus practical examples, go check out Section 5.8. It's giving required reading πβ¨
β COMPATIBILITY β
CREATE POLICY is a PostgreSQL extension. That's it, that's the whole section. Periodt π
π
π¦ SEE ALSO π¦
ALTER POLICY(ALTER_POLICY(7))DROP POLICY(DROP_POLICY(7))ALTER TABLE(ALTER_TABLE(7))
Go read those next, bestie, they're iconic too ππ₯
Okay that's the whole page, mwah π This masterpiece is from PostgreSQL 14.24, dated 2026, and it's the CREATE POLICY(7) manual page. Now go forth and secure those rows, queen πβ¨πΈ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms