🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Data Management / Database Administration › Database Security & Role Management › Lesson 3
TEC-DATA86-05 · Lesson 3 · 14 clock hours · lab

Role-Based Access Control: Privileges, Roles & Least Privilege

Objective. Learners design and implement a multi-tier RBAC schema in PostgreSQL using CREATE ROLE, GRANT, REVOKE, and row-level security (RLS) to enforce least-privilege access for at least three organizational roles.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

What Is Role-Based Access Control?

Role-Based Access Control (RBAC) is a security model in which permissions are assigned to roles rather than directly to individual users. A user then receives permissions by being made a member of a role. This approach keeps security policies consistent, easy to audit, and straightforward to change when job responsibilities shift. RBAC is a core concept in every major database platform and is tested on the PostgreSQL CE, Oracle DBA CA, Microsoft DP-300, and DAMA CDMP Associate exams.

The Principle of Least Privilege

Least privilege means every account or role is granted only the permissions it needs to perform its defined job — nothing more. A data-entry clerk who only inserts new patient intake records has no business deleting records or reading billing data. Granting excess privileges is one of the most common causes of data breaches, compliance violations (HIPAA, PCI-DSS), and accidental data loss. Always start with no privileges and add only what is explicitly required.

Safety note: In a production database, incorrectly granting elevated privileges — especially DELETE, TRUNCATE, or DROP — to an unprivileged role can lead to irreversible data loss. Before any privilege change on a live system, verify you are connected to the correct database, confirm the target role, and ensure a current backup exists. Treat a superuser session the way you would treat an energized electrical panel: use it only when necessary, close it immediately when done, and never leave a superuser psql session unattended.

PostgreSQL Roles and Inheritance

PostgreSQL uses a unified concept called a role for both users and groups. A role can log in (if `LOGIN` is granted) and can inherit privileges from parent roles (if `INHERIT` is set, which is the default).

```sql

-- Create a base role with no login

CREATE ROLE data_entry_clerk NOLOGIN;

-- Create a login account that belongs to that role

CREATE ROLE alice LOGIN PASSWORD 'changeme';

GRANT data_entry_clerk TO alice;

```

When Alice logs in, she automatically has whatever privileges `data_entry_clerk` holds, because inheritance is enabled by default.

GRANT and REVOKE

`GRANT` assigns a privilege on a database object to a role. `REVOKE` removes it.

```sql

-- Allow data_entry_clerk to insert into patients

GRANT INSERT ON patients TO data_entry_clerk;

-- Allow nurse_supervisor to select and update

GRANT SELECT, UPDATE ON patients TO nurse_supervisor;

-- Remove a privilege if it was granted in error

REVOKE DELETE ON patients FROM nurse_supervisor;

```

Common object-level privileges include `SELECT`, `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, `REFERENCES`, and `EXECUTE` (for functions). Schema-level `USAGE` must also be granted before a role can see objects inside a schema.

Role Hierarchy in the Hospital Scenario

For a hospital records database, a sensible hierarchy looks like this:

In practice, `dba_admin` maps to a role that is a member of the PostgreSQL built-in `pg_monitor` and has been granted explicit DDL rights — it is not the PostgreSQL superuser for day-to-day work.

Row-Level Security (RLS)

Object-level privileges control whether a role can touch a table at all. Row-Level Security (RLS) controls which rows within that table the role can see or modify. RLS must be explicitly enabled on each table:

```sql

-- Enable RLS on the patients table

ALTER TABLE patients ENABLE ROW LEVEL SECURITY;

-- A policy that lets nurses see only their ward's rows

CREATE POLICY nurse_ward_policy ON patients

FOR SELECT

TO nurse_supervisor

USING (ward_id = current_setting('app.current_ward')::int);

```

The `USING` clause is evaluated for every row. Only rows where the expression is true are returned. If no policy applies to a role, the default behavior after enabling RLS is to return zero rows (a safe default).

Table owners and superusers bypass RLS by default. To make RLS apply to the table owner as well, use `ALTER TABLE patients FORCE ROW LEVEL SECURITY;`.

Testing With SET ROLE

During development you can impersonate a role within your session without logging out:

```sql

SET ROLE data_entry_clerk;

DELETE FROM patients WHERE patient_id = 1; -- should fail

RESET ROLE;

```

A correctly configured policy returns an error such as `ERROR: permission denied for table patients`. Document every error message during testing — the message itself is evidence that the policy is enforced.

Auditing and Logging

Always enable `pgaudit` or PostgreSQL's built-in `log_statement` parameter in a security-sensitive environment. Log all DDL and privilege changes. Without an audit trail, you cannot prove a policy was enforced at a given point in time — a requirement for HIPAA and other regulated environments.

Summary

RBAC in PostgreSQL rests on three pillars: well-designed roles with clear inheritance, precise `GRANT`/`REVOKE` statements, and Row-Level Security policies that limit row visibility. Applying least privilege at every layer — schema, table, and row — dramatically reduces your attack surface and supports compliance auditing.

Key terms8

RoleA named database entity in PostgreSQL that can hold privileges, be granted to users, and optionally have login capability; serves as both a user account and a group.
Least PrivilegeThe security principle that every role or account should be granted only the minimum permissions required to perform its defined function.
GRANTThe SQL statement used to assign a specific privilege on a database object (table, schema, function, etc.) to a role.
REVOKEThe SQL statement used to remove a previously granted privilege from a role.
Row-Level Security (RLS)A PostgreSQL feature that allows table-level policies to control which individual rows a role can read or modify, independent of object-level privileges.
InheritanceThe PostgreSQL role property (enabled by default) that causes a member role to automatically receive all privileges held by its parent role.
SET ROLEA PostgreSQL session command that switches the active role within a session to a specified role the current user is a member of, used for testing privilege boundaries.
Policy (RLS Policy)A named rule created with CREATE POLICY that defines a USING or WITH CHECK expression controlling row visibility or modification for a specified role and command type.
In class: Design activity: given a scenario (a hospital records database with roles: data_entry_clerk, nurse_supervisor, auditor, dba_admin), learners draw an entity-relationship-style privilege diagram showing which role inherits from which and which objects each role can SELECT/INSERT/UPDATE/DELETE. · Hands-on lab: learners implement their design in PostgreSQL — create a sample schema with three tables, create the four roles, grant appropriate privileges, enable RLS on the patient table so nurses see only their ward's rows, and test by switching session role with SET ROLE. · Peer review: pairs swap scripts and attempt to perform an unauthorized action (e.g., a data_entry_clerk deleting a record); they document the error message returned and write a one-paragraph explanation of why it confirms the policy is enforced.
Materials: PostgreSQL 16 documentation (GRANT, CREATE ROLE, Row Security Policies chapters), sample hospital schema SQL script (provided), Linux VMs, psql client
Check: Instructor-run test script performs five privilege checks against the learner's database; learner must block all three unauthorized operations and permit all two authorized ones. Pass = 5 of 5 checks correct.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

PostgreSQL CE — Security and Role Management
1. A data_entry_clerk role has been granted INSERT on the patients table but no other privileges. When a session running as data_entry_clerk executes SELECT * FROM patients, what is the most likely result?
PostgreSQL CE — Security and Role Management
2. Which SQL statement would you use to prevent the nurse_supervisor role from deleting rows in the clinical_notes table after that privilege was mistakenly granted?
PostgreSQL CE — Security and Row-Level Security
3. A hospital DBA enables Row-Level Security on the patients table and creates a policy for the nurse_supervisor role. No policy is created for the auditor role. After RLS is enabled, what rows will the auditor role see when it queries the patients table?
PostgreSQL CE — Security and Role Management
4. In the hospital RBAC design, nurse_supervisor inherits privileges from data_entry_clerk. What does role inheritance mean in PostgreSQL?
PostgreSQL CE — Security and Role Management
5. Which command sequence correctly creates a role called auditor with no login capability and then grants it SELECT on the schema named hospital?
PostgreSQL CE — Security and Role Management
6. During a peer-review test, a session running as data_entry_clerk attempts DELETE FROM patients WHERE patient_id = 5 and receives the error ERROR: permission denied for table patients. What does this error confirm?
PostgreSQL CE — Security and Role Management
7. A DBA needs to test whether a newly created nurse_supervisor role has the correct privileges without logging out of the admin session. Which command should the DBA use?
DAMA CDMP — Data Security and Privacy
8. From a data governance perspective, which practice best supports demonstrating HIPAA compliance for a hospital database's access control?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.