Role-Based Access Control (RBAC) is a security model used to manage permissions in databases. Instead of assigning permissions directly to users, RBAC groups users into roles, and each role is granted specific permissions. This approach simplifies access management, enhances security, and ensures users only have access to the data and operations they need.
In this tutorial, we will cover:
RBAC is widely used in organizations to:
MySQL provides user roles that simplify permission management.
CREATE ROLE read_only;
CREATE ROLE editor;
CREATE ROLE admin;GRANT SELECT ON database_name.* TO read_only;
GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO editor;
GRANT ALL PRIVILEGES ON database_name.* TO admin;GRANT read_only TO user1;
GRANT editor TO user2;
GRANT admin TO user3;SET ROLE read_only;Best Practice: Use roles instead of assigning direct privileges to users.
PostgreSQL has a robust role-based access system where roles can be both users and groups.
CREATE ROLE read_only;
CREATE ROLE editor;
CREATE ROLE admin;GRANT CONNECT ON DATABASE mydb TO read_only, editor, admin;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO editor;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO admin;CREATE USER user1 WITH PASSWORD 'password123';
GRANT read_only TO user1;
CREATE USER user2 WITH PASSWORD 'password456';
GRANT editor TO user2;ALTER USER user1 SET ROLE read_only;Best Practice: Use pg_roles to manage and audit role permissions.
SQL Server implements RBAC using database roles.
CREATE ROLE read_only;
CREATE ROLE editor;
CREATE ROLE admin;GRANT SELECT ON SCHEMA::dbo TO read_only;
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::dbo TO editor;
GRANT CONTROL ON SCHEMA::dbo TO admin;CREATE LOGIN user1 WITH PASSWORD = 'password123';
CREATE USER user1 FOR LOGIN user1;
ALTER ROLE read_only ADD MEMBER user1;
CREATE LOGIN user2 WITH PASSWORD = 'password456';
CREATE USER user2 FOR LOGIN user2;
ALTER ROLE editor ADD MEMBER user2;Best Practice: Use sys.database_principals to track user roles and permissions.
To list all roles and users assigned to them:
SELECT * FROM mysql.db WHERE user='user1';SELECT grantee, privilege_type FROM information_schema.role_table_grants;SELECT dp.name AS RoleName, m.name AS UserName
FROM sys.database_role_members drm
JOIN sys.database_principals dp ON drm.role_principal_id = dp.principal_id
JOIN sys.database_principals m ON drm.member_principal_id = m.principal_id;To revoke a role from a user:
REVOKE read_only FROM user1;REVOKE read_only FROM user1;ALTER ROLE read_only DROP MEMBER user1;Role-Based Access Control (RBAC) is a powerful mechanism for managing database security. It simplifies user access management, reduces security risks, and ensures only authorized users can access sensitive data.
By implementing RBAC correctly in MySQL, PostgreSQL, and SQL Server, organizations can enhance security while maintaining flexibility in access control.
Sign in to join the discussion and post comments.
Sign in