Manage your Row-level Security Policies as Code with Atlas v0.25
Hi everyone,
Thanks for joining us today for our v0.25 release announcement! In this version we are introducing a new feature that has been requested by many of you: support for Row-level Security Policies in PostgreSQL.
Additionally, we have made some minor changes to our pricing plans, more on that below.
What are Row-level Security Policies?
Row-level security (RLS) in PostgreSQL allows tables to have policies that restrict which rows can be accessed or
modified based on the user's role, enhancing the SQL-standard privilege system available through GRANT
.
When enabled, all normal access to the table must comply with these policies, defaulting to a deny-all approach if no policies are set, ensuring that no rows are visible or modifiable. Policies can be specific to commands, roles, or both, providing fine-grained control over data access and modification.
How does RLS work?
When you create and enable a row-level security (RLS) policy in PostgreSQL, the database enforces the specified access control rules on a per-row basis.
For example, you can create a policy that allows only employees to see their own records in an employees table. The policy could look like this:
CREATE POLICY employee_policy ON employees
FOR SELECT
USING (current_user = employee_role);
This SQL command creates an RLS policy named employee_policy
on the employees table. The FOR SELECT
clause specifies
that this policy applies to SELECT
queries. The USING
clause contains the condition current_user = employee_role
, which
means that a user can only select rows where the employee_role column matches their PostgreSQL username.
Next, database administrators typically run:
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
This command enables RLS on the employees table. With RLS enabled, PostgreSQL will check the policies defined for this table whenever a user attempts to access or modify existing rows, or insert new ones.
When a user executes a SELECT
query on the employees table, PostgreSQL evaluates the employee_policy
. If the user's
PostgreSQL role (username) matches the employee_role column value in a row, the row is included in the query result.
Otherwise, the row is excluded.
For instance, if the employees table contains the following data:
id | name | employee_role |
---|---|---|
1 | Alice | alice |
2 | Bob | bob |
3 | Charlie | charlie |
When the user alice runs SELECT * FROM employees
, PostgreSQL applies the policy:
SELECT * FROM employees WHERE current_user = employee_role;
This results in:
id | name | employee_role |
---|---|---|
1 | Alice | alice |
By enforcing these policies, RLS ensures that users only have access to the data they are permitted to see, enhancing the security and privacy of the database.