
In a PostgreSQL server, a role is a collection of permissions that you can assign to one or more users. Roles simplify assigning bundled privileges to users in a multi-user environment using a single statement.
One great example of a scenario where you can use database roles is an e-commerce application to process orders. In this scenario, you have three roles: system administrators, order specialists, and customer support.
In the above example, assume you have three store administrators, seven order specialists, and 15 customer support staff in your company. Without roles, you would have to manually assign the permissions to each user. However, with the role-based model, you only need to create three roles to assign privileges to the users.
In this guide, you'll implement role-based permissions on the PostgreSQL database on your Ubuntu 20.04 server.
To complete this tutorial, you need:
SSH to your server and follow the steps below to create a sample database and table.
Then, log in to your PostgreSQL database server as postgres.
Enter your postgres user password and press Enter to proceed. Then, execute the SQL command below to create a store_db database.
Switch to the new store_db database.
Next, create an orders table.
Insert the following sample records into the orders table.
Ensure the records are in place by executing a SELECT statement against the orders table.
You should get the following output.
You've successfully set up a store_db database and created an orders table. In the next step, you'll define some roles and permissions.
The PostgreSQL server treats roles as entities that can own database objects and permissions. Therefore, every role you create in the PostgreSQL database server is valid across all databases.
The following is the basic syntax for defining database roles in a PostgreSQL server.
There are many options that you can define when creating the roles, but they're beyond the scope of this guide.
In this tutorial, you'll create sample roles and later associate them to different users depending on the privileges you want them to inherit.
Create the three roles.
After executing each command, you'll get the following output.
Ensure the roles are in place by listing them.
You should get the following output. Please note the postgres role is a default system role.
With the system roles created, you'll grant permissions Using the following syntax.
Start by assigning INSERT, SELECT, UPDATE, and DELETE permissions to the STORE_ADMIN role. In other words, any member of the STORE_ADMIN role can execute all CRUD operations against the orders table.
Also, for the INSERT command to work, you have to grant privileges to the currval and nextval functions which are responsible for sequence manipulation in the auto-increment/SERIAL columns. The order_id is a SERIAL column in the orders table in this tutorial.
Next, assign INSERT and SELECT permissions to the ORDER_SPECIALIST role. In simple terms, members of the ORDER_SPECIALIST role can create and view orders, but if they want to update or delete the orders, they've to escalate the tasks to the STORE_ADMIN users.
Then assign the SELECT permission to the CUSTOMER_SUPPORT role. Members under this group can only list the orders, but you'll not allow them to change any records.
After each GRANT command, you should get the following output to confirm the new change.
Read the grants table to ensure you've set up the permissions correctly.
The following output shows the roles and the associated permissions denoted with a(INSERT/APPEND), r(SELECT/READ), w(UPDATE/WRITE), d(DELETE), and U(USAGE).
You've now set the appropriate roles and assigned the correct privileges. You'll create users and associate them with the roles in the next step.
You'll log in and interact with the database using user accounts and not roles in your database. Therefore, you need to create users to make operations to the orders table.
To associate users with privileges, you'll assign them to the different roles you've already defined.
Create 6 user accounts named john, mary, isaac, jane, jacob, and carol. Replace EXAMPLE_PASSWORD with a strong password for each user.
Next, assign the STORE_ADMIN role to users john and mary.
Then, associate user isaac, jane, and jacob to the ORDER_SPECIALIST role.
Then link user carol to the CUSTOMER_SUPPORT role.
Log out from the PostgreSQL database.
You've now created users and assigned them appropriate permissions through roles. In the next step, you'll test whether everything is working as expected.
Log in to the PostgreSQL server either as user john or mary. Remember, both of these accounts have the STORE_ADMIN privileges: INSERT, SELECT, UPDATE, and DELETE.
or.
Enter the password for user john or mary and press Enter to proceed. Then, execute the following statement.
INSERT statement:
Output.
UPDATE statement:
Output.
DELETE statement:
Output.
SELECT statement:
Output.
The privileges for the STORE_ADMIN role are working without any problems. Exit from the PostgreSQL database.
Next, log in as user isaac, jane, or jacob. Remember, these users have the ORDER_SPECIALIST permissions.
or.
or.
Enter the password for user isaac, jane, or jacob and press Enter to proceed. Then, try executing the following commands.
INSERT statement:
Output.
UPDATE statement:
Output.
DELETE statement:
Output.
SELECT statement:
Output.
As you can see from the above outputs, only the INSERT and SELECT statements succeeded. Exit from the PostgreSQL database server.
Next, log in as user carol. This user has the CUSTOMER_SUPPORT privileges.
Then, enter the password for user carol and press Enter to proceed.
Try executing the following statement against the orders table.
INSERT statement:
Output.
UPDATE statement:
Output.
DELETE statement:
Output.
SELECT statement:
Output.
From the database responses above, only the SELECT statement worked for user carol. Your PostgreSQL database roles and permissions are now working as expected. You can merge the appropriate permissions to the respective users through roles.
You've set up a sample database and a table in this guide. You've also created database users, roles, and permissions on your PostgreSQL database.
Follow the links below to read more guides about the PostgreSQL database.
0 Comments
Be the first to comment and share your perspective with the community.