Introduction
In this lab, you will explore PostgreSQL role and permission management. The primary goal is to learn how to create roles with specific attributes and manage database privileges.
You will begin by creating a role named reporting_role with the NOLOGIN attribute, preventing it from directly connecting to the database. Then, you'll verify the role's creation. Subsequent steps will cover granting and testing database privileges, switching users to verify access, and revoking privileges from a role.
Create a Role
In this step, you will learn how to create a role in PostgreSQL. Roles are used to manage database access and permissions.
Connect to the PostgreSQL server:
Open a terminal and connect to the PostgreSQL server using the
psqlcommand-line tool as thepostgresuser.sudo -u postgres psqlYou should see a prompt similar to:
postgres=#Create the
reporting_rolerole:Now, let's create the
reporting_roleusing theCREATE ROLEcommand. We will specify theNOLOGINattribute, which means this role cannot be used to directly connect to the database. It will only be used to grant permissions to other roles or users.CREATE ROLE reporting_role NOLOGIN;This command creates the role. You should see the following output:
CREATE ROLEVerify the role creation:
To verify that the role has been created, you can use the
\ducommand (short for\list users). This command lists all roles in the database.\duYou should see
reporting_rolein the list of roles. The output will look something like this:List of roles Role name | Attributes | Member of ----------------+------------------------------------------------------------+----------- labex | Superuser | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} reporting_role | Cannot login | {}Exit the psql shell.
\q
Grant Database Privileges
In this step, you will learn how to grant database privileges to a role. We'll build upon the reporting_role created in the previous step.
Connect to the PostgreSQL server:
Connect to the PostgreSQL server using the
psqlcommand-line tool as thepostgresuser.sudo -u postgres psqlCreate a database:
Let's create a database named
reporting_dbthat ourreporting_rolewill have access to.CREATE DATABASE reporting_db;You should see the following output:
CREATE DATABASEConnect to the
reporting_dbdatabase:Connect to the newly created database:
\c reporting_dbThe prompt should change to
reporting_db=#.Create a table:
Let's create a simple table named
sales_datawithin thereporting_dbdatabase.CREATE TABLE sales_data ( id SERIAL PRIMARY KEY, product VARCHAR(255), amount DECIMAL );You should see the following output:
CREATE TABLEGrant SELECT privilege on the
sales_datatable to thereporting_role:Now, we will grant the
SELECTprivilege on thesales_datatable to thereporting_role. This will allow any role or user that is a member ofreporting_roleto read data from thesales_datatable.GRANT SELECT ON sales_data TO reporting_role;You should see the following output:
GRANTExit the psql shell.
\q
Create User and Test Privileges
In this step, you will create a user and grant it membership to the reporting_role to test the privileges.
Connect to the PostgreSQL server:
Connect to the PostgreSQL server using the
psqlcommand-line tool as thepostgresuser.sudo -u postgres psqlConnect to the
reporting_dbdatabase:Connect to the
reporting_dbdatabase:\c reporting_dbCreate a user and grant membership to the
reporting_role:Let's create a new user named
report_user. Then, we'll grant this user membership to thereporting_role. This meansreport_userwill inherit the privileges granted toreporting_role.CREATE USER report_user; GRANT reporting_role TO report_user;You should see the following output:
CREATE ROLE GRANT ROLEConfigure PostgreSQL for Passwordless Authentication:
By default, PostgreSQL uses
peerauthentication, which can cause connection issues in this lab. We will change it totrustto simplify access for thereport_user.First, exit the
psqlshell:\qNow, modify the
pg_hba.conffile to change the authentication method. Thesedcommand below will back up the original file aspg_hba.conf.bakand replacepeerwithtrustfor local connections.sudo sed -i.bak 's/^local\s\+all\s\+all\s\+peer/local all all trust/' /etc/postgresql/14/main/pg_hba.confReload PostgreSQL to apply the changes:
sudo service postgresql reloadInsert data into the
sales_datatable:Connect back to the database as
postgresuser.sudo -u postgres psql -d reporting_dbInsert some sample data into the
sales_datatable for testing purposes.INSERT INTO sales_data (product, amount) VALUES ('Laptop', 1200.00), ('Mouse', 25.00), ('Keyboard', 75.00);You should see the following output:
INSERT 0 3Exit the psql shell.
\qConnect to the
reporting_dbdatabase as thereport_user:Open a new terminal window and use the following command:
psql -d reporting_db -U report_userYou will be connected to the database as
report_userwithout a password prompt.Test the privileges:
Now, try to select data from the
sales_datatable:SELECT * FROM sales_data;You should see the data you inserted:
id | product | amount ----+----------+-------- 1 | Laptop | 1200.00 2 | Mouse | 25.00 3 | Keyboard | 75.00 (3 rows)If you see this output, it means the
report_userhas theSELECTprivilege on thesales_datatable, which was granted through thereporting_role.Exit the psql shell.
\q
Revoke Privileges
In this step, you will revoke the SELECT privilege from the reporting_role and verify that the report_user no longer has access to the sales_data table.
Connect to the PostgreSQL server as the
postgresuser:Switch back to the terminal where you are connected as the
postgresuser. Connect to the PostgreSQL server using thepsqlcommand-line tool as thepostgresuser.sudo -u postgres psqlConnect to the
reporting_dbdatabase:Connect to the
reporting_dbdatabase:\c reporting_dbRevoke the SELECT privilege from the
reporting_role:Now, we will revoke the
SELECTprivilege on thesales_datatable from thereporting_role.REVOKE SELECT ON sales_data FROM reporting_role;You should see the following output:
REVOKEExit the psql shell.
\qConnect to the
reporting_dbdatabase as thereport_user:Switch back to the terminal where you are connected as the
report_user.psql -d reporting_db -U report_userVerify access denial as
report_user:As the
report_user, run theSELECTquery again to verify that you no longer have access to the data in thesales_datatable:SELECT * FROM sales_data;You should see an error message similar to:
ERROR: permission denied for table sales_dataThis confirms that the
report_userno longer hasSELECTprivileges on thesales_datatable because the privilege was revoked from thereporting_role.Exit the psql shell.
\q
Summary
In this lab, you have learned how to create roles, grant privileges, create users, assign roles to users, and revoke privileges in PostgreSQL. These are fundamental concepts for managing database security and access control.


