🚧 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 › Web & Software Development › SQL and PostgreSQL Database Development › Lesson 10
TEC-WEB46-04 · Lesson 10 · 13 clock hours · lab

PostgreSQL Administration: Users, Roles, Backup, and Restore

Objective. Students will create database roles with appropriate privileges, perform a full database backup using pg_dump, and restore it to a new database.

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

PostgreSQL Administration: Users, Roles, Backup, and Restore

A production database without access controls and a tested backup strategy is a liability. This lesson covers two of the most important administration tasks you will perform as a developer or junior database administrator: controlling who can do what through PostgreSQL's role system, and protecting data through reliable backup and restore procedures.

Roles and Privileges in PostgreSQL

PostgreSQL uses roles as the single concept for both users and groups. A role can log in (making it behave like a user), own database objects, and be a member of other roles (making it behave like a group). This flexibility lets you model real-world permission structures cleanly.

Create a role with the `CREATE ROLE` statement. By default, a new role cannot log in. Add `LOGIN` to allow it, and `PASSWORD` to set a credential:

```sql

CREATE ROLE read_only LOGIN PASSWORD 'changeme';

CREATE ROLE read_write LOGIN PASSWORD 'changeme';

CREATE ROLE admin LOGIN PASSWORD 'changeme' CREATEDB;

```

Roles alone do nothing until you grant privileges. Use `GRANT` to give a role permission on specific database objects:

```sql

-- read_only: SELECT on all tables in public schema

GRANT CONNECT ON DATABASE library TO read_only;

GRANT USAGE ON SCHEMA public TO read_only;

GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only;

-- read_write: SELECT, INSERT, UPDATE, DELETE

GRANT CONNECT ON DATABASE library TO read_write;

GRANT USAGE ON SCHEMA public TO read_write;

GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO read_write;

GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO read_write;

-- admin: all privileges

GRANT ALL PRIVILEGES ON DATABASE library TO admin;

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO admin;

```

Verify each role by connecting with `psql -U read_only -d library` and attempting both permitted and restricted operations. Record every result in a security matrix — a table listing each role, each operation tested, and whether it succeeded or was correctly denied. This document becomes evidence that your access controls work as designed.

Remove a privilege with `REVOKE`. The principle of least privilege applies: grant only what the role needs to do its job, nothing more.

Backup with pg_dump

`pg_dump` is PostgreSQL's standard logical backup utility. It exports a consistent snapshot of one database without locking out other users, making it safe to run against a live system.

Two output formats matter most:

Plain-text SQL produces a `.sql` file you can read in any text editor and restore with `psql`:

```bash

pg_dump -U postgres -d library -f library_backup.sql

```

Custom format (flag `-Fc`) produces a compressed binary file restorable only with `pg_restore`. It supports parallel restores and selective object restoration, making it the preferred format for real backups:

```bash

pg_dump -U postgres -Fc -d library -f library_backup.dump

```

After the dump completes, always verify it. For a plain-text dump, check that the file begins with `-- PostgreSQL database dump` and that the file size is larger than zero. For a custom-format dump, run:

```bash

pg_restore --list library_backup.dump | head -20

```

This lists the objects stored in the archive. If the command returns output, the file is structurally valid.

Restore with pg_restore

Restoring the custom-format dump to a new database demonstrates that your backup is actually usable — the most critical test in any backup strategy.

```bash

createdb -U postgres library_restored

pg_restore -U postgres -d library_restored library_backup.dump

```

After restore, connect and run row-count queries to confirm data integrity:

```sql

SELECT relname, n_live_tup

FROM pg_stat_user_tables

ORDER BY relname;

```

Compare counts against the source database. Mismatches indicate a problem with the backup or restore process.

Writing an Administration Runbook

A runbook documents operational procedures so any qualified developer can perform them without guessing. A database runbook for the library system should cover:

Peer-review each other's runbooks against a checklist. A runbook that is ambiguous or incomplete will fail at the worst possible moment.

Safety and Professional Practice

Database administration carries real risk. Always test restores in a non-production environment before relying on a backup. Never store database passwords in plain-text scripts checked into version control; use environment variables or a secrets manager. Grant the `SUPERUSER` attribute only when absolutely necessary and audit its use. Keep backup files in a location separate from the server they back up — a backup stored only on the server it protects is not a backup.

Key terms9

RoleA PostgreSQL entity that can own database objects and hold privileges; it behaves as a user when given the LOGIN attribute, or as a group when granted to other roles.
GRANTA SQL statement that gives a role permission to perform specific operations (such as SELECT or INSERT) on database objects.
REVOKEA SQL statement that removes a previously granted privilege from a role.
Principle of Least PrivilegeA security practice in which each role or account is given only the minimum permissions required to perform its intended function.
pg_dumpThe PostgreSQL command-line utility that exports a consistent logical backup of a single database to a file without interrupting other users.
pg_restoreThe PostgreSQL command-line utility used to restore a database from a custom-format, directory-format, or tar-format dump file created by pg_dump.
Custom Format (-Fc)A compressed, binary pg_dump output format that supports parallel restores and selective object restoration; it can only be read by pg_restore, not by psql directly.
RunbookA documented set of step-by-step procedures that allow any qualified operator to perform routine or emergency administration tasks consistently and correctly.
Security MatrixA table that records each role, each operation tested against the database, and whether the result matched the intended access policy (permitted or denied).
In class: Create three roles (read_only, read_write, admin) using CREATE ROLE and GRANT statements; verify access by connecting to the database as each role and attempting permitted and restricted operations, recording results in a security matrix. · Use pg_dump to export the library database in both plain-text SQL and custom (-Fc) formats; verify the dump file is valid by checking its header and size, then restore the custom-format dump to a new library_restored database using pg_restore. · Write a one-page administration runbook documenting the backup schedule, restore procedure, and role definitions for the library database as if handing it off to a new developer; peer-review for completeness and accuracy.
Materials: PostgreSQL pg_dump and pg_restore documentation, psql, lab workstations, administration runbook template, instructor security matrix worksheet
Check: Instructor executes a live restore from the student's .dump file into a clean database and runs a row-count verification script; pass = all tables restore with correct row counts and the role security matrix is complete and accurate.

Practice check8

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

Security and access controls
1. A developer needs a PostgreSQL role that can connect to the library database and run SELECT queries but must never be able to modify data. Which statement correctly grants only the necessary privilege on all existing tables in the public schema?
Database backup and recovery
2. Which pg_dump flag produces a compressed binary file that can be selectively restored with pg_restore?
Database backup and recovery
3. After running pg_restore to load a backup into library_restored, a developer wants to verify data integrity. Which approach is most appropriate?
Professional and operational practice
4. A new developer is handed a database with no documentation. Which item is MOST important to include in an administration runbook to allow that developer to recover from a failure?
Security and access controls
5. A role named read_write already has SELECT and INSERT granted. A developer now runs REVOKE INSERT ON ALL TABLES IN SCHEMA public FROM read_write. What is the result?
Database backup and recovery
6. Which command allows a developer to inspect the list of objects stored inside a custom-format dump file without performing a full restore?
Professional and operational practice
7. A company stores its only copy of daily database backups on the same physical server as the PostgreSQL instance. Which risk does this practice fail to mitigate?
Security and access controls
8. By default, when a new role is created with CREATE ROLE rolename; without any additional options, which capability does the role have?
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.