Creates a data redaction policy for a table.
1 2 3 4 | CREATE REDACTION POLICY policy_name ON table_name [ { AFTER | BEFORE } old_policy_name ] [INHERIT] [ WHEN (when_expression) ] [ ADD COLUMN column_name WITH redaction_function_name ( [ argument [, ...] ] )] [, ... ]; |
Specifies the name of a redaction policy.
Specifies the name of the table to which the redaction policy is applied.
Specifies the relative location where the current policy is created. Generally, one masking policy is used for one table. By default, the current policy is created after the last candidate policy of the target table recorded in the current system catalog.
Specifies whether the masking policy is inherited from other masking policies. This parameter is not recommended.
Specifies the expression used for the redaction policy to take effect. The redaction policy takes effect only when this expression is true.
When a query statement is querying a table where a redaction policy is enabled, the redacted data is invisible in the query only if the WHEN expression for the redaction policy is true. Generally, the WHEN clause is used to specify the users for which the redaction policy takes effect.
The WHEN clause must comply with the following rules:
Specifies the name of the table column to which the redaction policy is applied.
Specifies the redaction function applied to the specified table column.
Specifies the list of arguments of the redaction function.
You can use the built-in masking functions MASK_NONE, MASK_FULL, and MASK_PARTIAL, or create your own masking functions by using the C language or PL/pgSQL. For details, see Data Redaction Functions.
Create redaction policy for a specified user.
1 2 | CREATE ROLE alice PASSWORD '{password}'; CREATE ROLE matu PASSWORD '{password}'; |
1 2 | CREATE TABLE emp(id int, name varchar(20), salary NUMERIC(10,2)); INSERT INTO emp VALUES(1, 'July', 1230.10), (2, 'David', 999.99); |
1 | CREATE REDACTION POLICY mask_emp ON emp WHEN(current_user = 'matu') ADD COLUMN salary WITH mask_full(salary); |
1 | GRANT SELECT ON emp TO matu; |
1 | SET ROLE matu PASSWORD '{password}'; |
1 | SELECT * FROM emp; |
Create redaction policy for the role.
1 | CREATE ROLE redact_role PASSWORD '{password}'; |
1 | GRANT redact_role to matu,alice; |
1 2 | CREATE TABLE emp1(id int, name varchar(20), salary NUMERIC(10,2)); INSERT INTO emp1 VALUES(3, 'Rose', 2230.20), (4, 'Jack', 899.88); |
1 | CREATE REDACTION POLICY mask_emp1 ON emp1 WHEN(pg_has_role(current_user, 'redact_role', 'member')) ADD COLUMN salary WITH mask_full(salary); |
If no user is specified, the current user (current_user) is used by default.
1 | CREATE REDACTION POLICY mask_emp1 ON emp1 WHEN (pg_has_role('redact_role', 'member')) ADD COLUMN salary WITH mask_full(salary); |
1 | GRANT SELECT ON emp1 TO matu; |
1 | SET ROLE matu PASSWORD '{password}'; |
1 | SELECT * FROM emp1; |