Configure Row-Level Security Policy
Row-level security (RLS) policy allows the table owner to define access policies that control users' access to specific rows of the table. When a user tries to query or update a table, the RLS policy will be applied first before any user command is executed to truncate the rows in the table.
Row-level security policies can be created for specific commands, such as SELECT
/INSERT
/UPDATE
/DELETE
, or for all commands (ALL
). Row-level security policies can also control access to specific rows in a table based on certain users, user groups, or according to specific conditions.
Row-Level Security Policy Overview
-
By default, no row-level security policy is set for a table. If a user has access to the table according to the SQL permission system, all rows in the table can be queried or updated.
-
Users can enable row-level security policies on a table using the
ALTER TABLE ... ENABLE ROW LEVEL SECURITY
command. After the policy is enabled, no other users except the table owner will be able to access and modify the table. Appropriate security policies must be set before other users can access and manipulate rows within the table.备注Operations that apply to the entire table (such as
TRUNCATE
andREFERENCES
) are not restricted by row-level security. -
Row-level security policies can be specific to commands, user roles, or both. A policy can be applied to all commands, or just to
SELECT
,INSERT
,UPDATE
, orDELETE
individually. Multiple roles can be granted the same policy, and the usual role hierarchy and inheritance rules also apply to row-level security policies. -
Superusers and roles with the
BYPASSRLS
attribute are not subject to row-level security policies. The table owner is typically not constrained by the policy, but row-level security can be enforced on the table owner by usingALTER TABLE ... FORCE ROW LEVEL SECURITY
. -
Only the table owner can enable, disable, or add row-level security policies.