-
Connect to the PostgreSQL server:
Connect to the PostgreSQL server using the psql command-line tool as the postgres user.
sudo -u postgres psql
-
Connect to the reporting_db database:
Connect to the reporting_db database:
\c reporting_db
-
Create 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 the reporting_role. This means report_user will inherit the privileges granted to reporting_role.
CREATE USER report_user;
GRANT reporting_role TO report_user;
You should see the following output:
CREATE ROLE
GRANT ROLE
-
Configure PostgreSQL for Passwordless Authentication:
By default, PostgreSQL uses peer authentication, which can cause connection issues in this lab. We will change it to trust to simplify access for the report_user.
First, exit the psql shell:
\q
Now, modify the pg_hba.conf file to change the authentication method. The sed command below will back up the original file as pg_hba.conf.bak and replace peer with trust for local connections.
sudo sed -i.bak 's/^local\s\+all\s\+all\s\+peer/local all all trust/' /etc/postgresql/14/main/pg_hba.conf
Reload PostgreSQL to apply the changes:
sudo service postgresql reload
-
Insert data into the sales_data table:
Connect back to the database as postgres user.
sudo -u postgres psql -d reporting_db
Insert some sample data into the sales_data table 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 3
Exit the psql shell.
\q
-
Connect to the reporting_db database as the report_user:
Open a new terminal window and use the following command:
psql -d reporting_db -U report_user
You will be connected to the database as report_user without a password prompt.
-
Test the privileges:
Now, try to select data from the sales_data table:
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_user has the SELECT privilege on the sales_data table, which was granted through the reporting_role.
Exit the psql shell.
\q