ALTER ROW LEVEL SECURITY POLICY

    注意事项

    表的所有者或管理员用户才能进行此操作。

    参数说明

    • policy_name

      行访问控制策略名称。

    • 行访问控制策略的表名。

    • new_policy_name

      新的行访问控制策略名称。

    • 行访问控制策略应用的数据库用户,可以指定多个用户,PUBLIC表示应用到所有用户。

    • using_expression

      行访问控制的表达式,返回值为boolean类型。

    1. postgres=# CREATE TABLE all_data(id int, role varchar(100), data varchar(100));
    2. --创建行访问控制策略,当前用户只能查看用户自身的数据
    3. postgres=# CREATE ROW LEVEL SECURITY POLICY all_data_rls ON all_data USING(role = CURRENT_USER);
    4. postgres=# \d+ all_data
    5. Table "public.all_data"
    6. Column | Type | Modifiers | Storage | Stats target | Description
    7. --------+------------------------+-----------+----------+--------------+-------------
    8. id | integer | | plain | |
    9. data | character varying(100) | | extended | |
    10. Row Level Security Policies:
    11. POLICY "all_data_rls"
    12. USING (((role)::name = "current_user"()))
    13. Has OIDs: no
    14. Location Nodes: ALL DATANODES
    15. Options: orientation=row, compression=no
    16. --修改行访问控制all_data_rls的名称
    17. postgres=# ALTER ROW LEVEL SECURITY POLICY all_data_rls ON all_data RENAME TO all_data_new_rls;
    18. --修改行访问控制策略影响的用户
    19. postgres=# ALTER ROW LEVEL SECURITY POLICY all_data_new_rls ON all_data TO alice, bob;
    20. postgres=# \d+ all_data
    21. Table "public.all_data"
    22. Column | Type | Modifiers | Storage | Stats target | Description
    23. --------+------------------------+-----------+----------+--------------+-------------
    24. id | integer | | plain | |
    25. role | character varying(100) | | extended | |
    26. data | character varying(100) | | extended | |
    27. Row Level Security Policies:
    28. TO alice,bob
    29. USING (((role)::name = "current_user"()))
    30. Has OIDs: no
    31. Options: orientation=row, compression=no, enable_rowsecurity=true
    32. --修改行访问控制策略表达式
    33. postgres=# ALTER ROW LEVEL SECURITY POLICY all_data_new_rls ON all_data USING (id > 100 AND role = current_user);
    34. postgres=# \d+ all_data
    35. Table "public.all_data"
    36. Column | Type | Modifiers | Storage | Stats target | Description
    37. --------+------------------------+-----------+----------+--------------+-------------
    38. id | integer | | plain | |
    39. role | character varying(100) | | extended | |
    40. data | character varying(100) | | extended | |
    41. Row Level Security Policies:
    42. POLICY "all_data_new_rls"
    43. TO alice,bob
    44. USING (((id > 100) AND ((role)::name = "current_user"())))
    45. Has OIDs: no
    46. Location Nodes: ALL DATANODES

    相关链接