0
0
PostgreSQLquery~10 mins

Role creation and management in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Role creation and management
Start
Create Role
Assign Privileges
Grant Role to User
Verify Role and Privileges
Modify or Drop Role
End
This flow shows how to create a role, assign privileges, grant it to users, verify, and modify or drop the role.
Execution Sample
PostgreSQL
CREATE ROLE analyst;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO analyst;
GRANT analyst TO alice;
\du
This code creates a role 'analyst', grants it select access on all tables in public schema, assigns it to user 'alice', and lists roles.
Execution Table
StepCommandEffectResult/Output
1CREATE ROLE analyst;Creates a new role named 'analyst'Role 'analyst' created
2GRANT SELECT ON ALL TABLES IN SCHEMA public TO analyst;Gives 'analyst' select rights on all public tablesPrivileges granted to 'analyst'
3GRANT analyst TO alice;Assigns role 'analyst' to user 'alice'Role 'analyst' granted to 'alice'
4\duLists all roles and their attributesList shows 'analyst' role and 'alice' with 'analyst' role assigned
5REVOKE analyst FROM alice; DROP ROLE analyst;Revokes role from alice then deletes the role 'analyst'Role 'analyst' dropped
6DROP ROLE analyst;Fails if role does not existERROR: role "analyst" does not exist
💡 Execution stops after role is dropped or error if role missing
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5
Rolesnoneanalyst createdanalyst with SELECT privilegeanalyst assigned to aliceanalyst dropped
Key Moments - 2 Insights
Why does the GRANT analyst TO alice; command not give alice any privileges directly?
Because 'analyst' is a role that holds privileges; alice gets those privileges by having the role assigned, not directly.
What happens if you try to DROP a role that is still assigned to a user?
PostgreSQL will prevent dropping the role until it is revoked from all users.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the effect of step 2?
AGrants SELECT privileges on all public tables to role 'analyst'
BCreates a new user named 'analyst'
CAssigns role 'analyst' to user 'alice'
DDeletes the role 'analyst'
💡 Hint
Check the 'Effect' column in step 2 of the execution table
At which step does user 'alice' get the 'analyst' role assigned?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Command' and 'Effect' columns for step 3
If you try to drop a role that does not exist, what is the result?
ARole is created automatically
BAn error is shown
CRole is dropped silently
DPrivileges are revoked
💡 Hint
See the 'Result/Output' column in step 6
Concept Snapshot
CREATE ROLE role_name; -- makes a new role
GRANT privileges TO role_name; -- gives rights to role
GRANT role_name TO user; -- assigns role to user
DROP ROLE role_name; -- deletes role if unused
Roles group privileges for easy management.
Full Transcript
This visual execution shows how to create and manage roles in PostgreSQL. First, a role is created with CREATE ROLE. Then privileges like SELECT on tables are granted to the role. Next, the role is assigned to a user with GRANT role TO user. The \du command lists roles and their assignments. Finally, roles can be dropped if no longer needed. Trying to drop a role still assigned to users causes an error. This step-by-step trace helps beginners see how roles group privileges and are assigned to users for easier permission management.