Objective. Students will implement an ongoing data-quality monitoring process using PostgreSQL triggers and scheduled queries, set up an audit log table, and produce a compliance summary document referencing applicable data-privacy principles.
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.
A database that stores accurate, trustworthy data is only as useful as the controls that keep it that way over time. Data quality degrades silently: NULL values accumulate, out-of-range entries slip through application bugs, and unauthorized changes go unnoticed. Two complementary mechanisms address this: audit logging records who changed what and when, while data quality monitoring checks whether the data still meets defined standards. Together they form the foundation of any compliance posture, whether the concern is an internal data governance policy or an external privacy framework.
A PostgreSQL trigger is a function that the database engine calls automatically when a specified event occurs on a table. For audit purposes you attach an `AFTER` trigger to `INSERT`, `UPDATE`, and `DELETE` operations. Inside the trigger function you capture the operation type, the acting database user (`SESSION_USER` or `CURRENT_USER`), the wall-clock timestamp (`NOW()`), and the old and new row values exposed through the `OLD` and `NEW` pseudo-records.
The audit records go into a dedicated `audit_log` table. That table should be append-only: grant `INSERT` to the trigger-owning role but revoke `UPDATE` and `DELETE` so that no one can tamper with historical records. Storing changed values as `JSONB` columns (`old_data`, `new_data`) keeps the schema generic and allows you to reuse the same trigger function across multiple target tables.
A minimal `audit_log` schema looks like this:
```sql
CREATE TABLE audit_log (
log_id BIGSERIAL PRIMARY KEY,
table_name TEXT NOT NULL,
operation TEXT NOT NULL, -- INSERT, UPDATE, DELETE
db_user TEXT NOT NULL,
changed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
old_data JSONB,
new_data JSONB
);
```
The trigger function returns `NULL` for `AFTER` triggers (the return value is ignored) and uses `TG_TABLE_NAME`, `TG_OP`, `ROW_TO_JSON(OLD)`, and `ROW_TO_JSON(NEW)` to populate each column.
pg_cron is a background worker extension for PostgreSQL that lets you schedule SQL statements using standard cron syntax directly inside the database. Enabling it requires adding `pg_cron` to `shared_preload_libraries` in `postgresql.conf` and restarting the server. Once loaded, the `cron.schedule()` function registers a job.
A daily quality-check script should test at minimum:
Results write to a `data_quality_log` table that includes a `pass_fail` flag (a simple `BOOLEAN` or `TEXT` column), the check name, the table checked, the count of violations found, and the run timestamp. Setting `pass_fail = FALSE` when the violation count exceeds zero gives operators a quick filter.
If the `pg_cron` extension is unavailable in your environment, an OS-level cron job on the Ubuntu VM can call `psql` with a `-f` flag pointing to your SQL script. Both approaches are acceptable; the key is that the job runs unattended on a schedule.
Data privacy frameworks worldwide share a set of core principles even when their legal texts differ. For this lesson you document how your technical controls support three of the most widely recognized:
Your one-page compliance summary must explain each principle in plain language, describe the specific technical control in your implementation that supports it, and note any gaps or limitations. This document is not a legal opinion; it is a technical governance artifact.
In a database environment, "safety" means protecting data integrity and system availability rather than physical hazards. Apply these professional practices:
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.
Does the school offer financial aid?
No. We don’t offer financial aid because we’re free. No, really: completely free.
Do you offer industry certification?
No. We are not certified yet, so we can’t offer that to students. What we do offer is knowledge and understanding of a trade.
Enroll first in the trade you’re interested in. You get your key, your program opens, and you begin studying.
Enroll free →