🚧 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 › PostgreSQL Administration › Lesson 6
TEC-DATA86-03 · Lesson 6 · 10 clock hours · lab

User Management, Roles, and Object Privileges

Objective. Students create roles and users, assign object privileges using GRANT/REVOKE, and verify access control behavior by testing connections as different database users.

⚠ 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.

Why Access Control Matters

A database that stores sensitive data — customer records, financial transactions, personnel files — must restrict who can read, change, or delete that data. PostgreSQL enforces access control through roles and privileges. Getting this right is not optional: unauthorized access or accidental data modification are among the leading causes of data breaches and compliance failures. The principle of least privilege requires that every user or process receives only the permissions needed to do its job and nothing more.

Roles vs. Users in PostgreSQL

In PostgreSQL, roles are the single unified concept for both users and groups. A role that has the LOGIN attribute is a database user. A role without LOGIN is a group role used to bundle privileges. This unified model means you assign permissions to a role once, then grant that role to any number of users — a far cleaner approach than setting permissions individually.

Common attributes you control when creating a role:

Creating Roles and Users

Create a group role (no login):

```sql

CREATE ROLE readonly_role;

CREATE ROLE data_entry_role;

CREATE ROLE dba_role;

```

Create users (roles with LOGIN):

```sql

CREATE ROLE alice WITH LOGIN PASSWORD 'secure_pass1';

CREATE ROLE bob WITH LOGIN PASSWORD 'secure_pass2';

CREATE ROLE carol WITH LOGIN PASSWORD 'secure_pass3';

CREATE ROLE dan WITH LOGIN PASSWORD 'secure_pass4';

```

Assign users to group roles:

```sql

GRANT readonly_role TO alice;

GRANT data_entry_role TO bob;

GRANT data_entry_role TO carol;

GRANT dba_role TO dan;

```

Granting Object Privileges

Privileges are granted on specific database objects — tables, views, sequences, functions. The core table privileges are SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, and TRIGGER.

```sql

-- readonly_role may only query

GRANT SELECT ON TABLE orders TO readonly_role;

-- data_entry_role may query and add/edit rows

GRANT SELECT, INSERT, UPDATE ON TABLE orders TO data_entry_role;

-- dba_role gets full access including delete

GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE orders TO dba_role;

```

Because Alice is a member of readonly_role, she inherits SELECT on orders automatically — she does not need a direct grant.

Testing Privileges with SET ROLE

You can impersonate a role without logging out by using `SET ROLE`. This is the standard technique for verifying that a role has exactly the access it should:

```sql

SET ROLE alice;

SELECT * FROM orders; -- should succeed

INSERT INTO orders ...; -- should fail

RESET ROLE; -- return to original role

```

Always `RESET ROLE` after testing so you do not accidentally remain in an impersonated context.

Revoking Privileges

Use REVOKE to remove permissions:

```sql

REVOKE DELETE ON TABLE orders FROM dba_role;

REVOKE data_entry_role FROM carol;

```

Revoking a role membership removes all privileges that role carried for that user.

Auditing Privileges

PostgreSQL provides catalog views and functions to inspect the current state of permissions:

Example audit query:

```sql

SELECT grantee, table_name, privilege_type

FROM information_schema.role_table_grants

WHERE table_schema = 'public'

ORDER BY grantee, table_name;

```

Document results in a privilege matrix: rows are users, columns are tables, cells list allowed operations.

Principle of Least Privilege and the Superuser Hazard

Superuser status in PostgreSQL is equivalent to root on a Linux system — it bypasses every privilege check. If a data_entry user is accidentally granted SUPERUSER, they can drop tables, read any schema, and alter roles. The fix:

```sql

ALTER ROLE bob NOSUPERUSER;

```

Verify immediately with:

```sql

SELECT rolname, rolsuper FROM pg_roles WHERE rolname = 'bob';

```

Document the before state (rolsuper = true) and the after state (rolsuper = false) so the incident is traceable. This audit trail practice aligns with DAMA DMBOK2 guidance on data security governance.

Safety and Professional Practice

Database administration does not involve physical hazards like electrical work, but it carries its own serious risks. Always work in a non-production environment when learning or testing privilege changes. Never share superuser credentials. Use strong, unique passwords and store them in a secrets manager rather than in scripts. Before revoking any privilege in a live system, confirm with a supervisor that downstream applications will not break. Maintain a change log of every GRANT and REVOKE for compliance and forensic purposes.

Key terms10

RoleA named database entity in PostgreSQL that can hold privileges and be granted to users or other roles; serves as both user account and group depending on its attributes.
LOGIN attributeA role attribute that permits the role to authenticate and open a database connection, making it function as a traditional user account.
GRANTThe SQL command used to assign a privilege on a database object to a role, or to assign one role as a member of another role.
REVOKEThe SQL command used to remove a previously granted privilege or role membership.
Principle of Least PrivilegeA security principle stating that each user or process should be given only the minimum permissions necessary to perform its intended function.
SUPERUSERA PostgreSQL role attribute that bypasses all access control checks; equivalent in power to a database root account and should be assigned sparingly.
SET ROLEA PostgreSQL command that causes the current session to adopt the permissions of the specified role, used to test what that role can and cannot do.
has_table_privilege()A PostgreSQL system function that returns TRUE or FALSE indicating whether a specified user holds a particular privilege on a specified table.
Privilege matrixA documentation artifact that maps each database user or role to each object and lists which operations (SELECT, INSERT, etc.) are permitted, used for auditing and compliance.
Role inheritanceThe mechanism by which a role automatically receives the privileges of any group role it has been granted membership in, unless the NOINHERIT attribute is set.
In class: Create a role hierarchy with three roles (readonly_role, data_entry_role, dba_role) and four users; assign roles using GRANT role TO user, then test each user's privileges with SET ROLE and attempt SELECT, INSERT, UPDATE, and DELETE on protected tables, logging results. · Use pg_roles, pg_user, information_schema.role_table_grants, and has_table_privilege() to audit current privileges; produce a privilege matrix spreadsheet mapping each user to allowed operations on each table. · Demonstrate and explain the principle of least privilege by reconfiguring a scenario where a data_entry user has been inadvertently granted superuser; revoke excess privileges, re-test, and document the before/after states.
Materials: PostgreSQL 16 VM, psql, privilege matrix spreadsheet template, PostgreSQL Documentation Chapter 21 (Database Roles) and Chapter 5 Section 7 (Privileges), DAMA DMBOK2 Chapter on Data Security (reference)
Check: Instructor runs a privilege audit script against the student's database and compares to the submitted privilege matrix; pass = matrix is accurate AND all four users have only their intended permissions confirmed by live testing.

Practice check8

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

PostgreSQL CE (EDB) — Role Management
1. A database administrator runs: GRANT readonly_role TO alice; What is the effect of this statement?
PostgreSQL CE (EDB) — Privilege Management
2. Which SQL command correctly removes the INSERT privilege on the table 'invoices' from the role 'data_entry_role'?
PostgreSQL CE (EDB) — Role Attributes
3. A junior DBA discovers that the user 'bob', who should only enter data, has rolsuper = true in pg_roles. Which command corrects this?
PostgreSQL CE (EDB) — Privilege Auditing
4. Which catalog view or function would you use to confirm programmatically whether user 'carol' has SELECT on the table 'products'?
PostgreSQL CE (EDB) — Session Management
5. After running SET ROLE readonly_role; in a psql session, what should the DBA do immediately after finishing privilege tests?
PostgreSQL CE (EDB) — Privilege Auditing
6. A security audit requires documenting all privileges held on tables in the public schema. Which view provides grantee, table name, and privilege type in a single query?
DAMA CDMP Associate — Data Security
7. According to the principle of least privilege, which permission set is most appropriate for a reporting user who only needs to run SELECT queries?
PostgreSQL CE (EDB) — Privilege Auditing
8. A DBA wants to verify which roles have been directly granted privileges on tables, not which individual users inherited them. Which approach is most appropriate?
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.