CREATE REDACTION POLICY

Function

CREATE REDACTION POLICY creates a data redaction policy for a table.

Precautions

Syntax

1
2
3
CREATE REDACTION POLICY policy_name ON table_name
    [ WHEN (when_expression) ]
    [ ADD COLUMN column_name WITH redaction_function_name ( [ argument [, ...] ] )] [, ... ];

Parameter Description

Examples

Create redaction policy for a specified user.

  1. Create users alice and matu:
    1
    2
    CREATE ROLE alice PASSWORD '{Password}';
    CREATE ROLE matu PASSWORD '{Password}';
    
  2. Create a table object emp as user alice, and insert data into the table.
    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);
    
  3. Create a redaction policy mask_emp for the emp table as user alice to make the salary column invisible to user matu.
    1
    CREATE REDACTION POLICY mask_emp ON emp WHEN(current_user = 'matu') ADD COLUMN salary WITH mask_full(salary);
    
  4. Grant the SELECT permission on the emp table to user matu as user alice.
    1
    GRANT SELECT ON emp TO matu;
    
  5. Switch to user matu.
    1
    SET ROLE matu PASSWORD '{Password}';
    
  6. Query the emp table. Data in the salary column has been redacted.
    1
    SELECT * FROM emp;
    

Create redaction policy for the role.

  1. Create a role redact_role.
    1
    CREATE ROLE redact_role PASSWORD '{Password}';
    
  2. Add users matu and alice to the role redact_role.
    1
    GRANT redact_role to matu,alice;
    
  3. Create a table object emp1 as the administrator and insert data.
    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);  
    
  4. Create a redaction policy mask_emp1 for the table object emp1 as the administrator to make the salary column invisible to role redact_role.
    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);
    
  5. The administrator grants the SELECT permission on the table emp1 to the user matu.
    1
    GRANT SELECT ON emp1 TO matu;
    
  6. Switch to user matu.
    1
    SET ROLE matu PASSWORD '{Password}';
    
  7. Query the emp1 table. Data in the salary column has been redacted.
    1
    SELECT * FROM emp1;
    

Helpful Links

ALTER REDACTION POLICY, DROP REDACTION POLICY