-
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
with a password (replace your_password
with a strong password). 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 WITH PASSWORD 'your_password';
GRANT reporting_role TO report_user;
You should see the following output:
CREATE ROLE
GRANT
-
Insert data into the sales_data
table:
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 -W
You will be prompted for the password you set for report_user
. Enter it.
-
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