Install Safegres and Audit a PGPM Workspace

Dan Lynch

Dan Lynch

lesson header image

Safegres checks the grants, row-level security (RLS), policies, and indexes deployed in PostgreSQL. In this course, it uses pgsql-test to deploy your PGPM workspace to a temporary database, audit it, and drop it when done.

Already have a database to scan? Audit it directly without PGPM.

Set up the workspace

Follow the prerequisites and use an empty PGPM workspace. The scaffold includes Safegres and pgsql-test. From its root, install the dependencies and create the lab module:

bash
pgpm init # Enter audit-demo when prompted
pnpm install

Or, if you're not running interactively (for example, as an agent):

bash
pgpm init --no-tty --moduleName audit-demo --moduleDesc 'Safegres audit demo'
pnpm install

Prepare the lab

bash
docker run --rm -d --name safegres-lab \
  -e POSTGRES_PASSWORD=lab-only \
  -p 127.0.0.1:55432:5432 constructiveio/postgres-plus:18
export PGHOST=127.0.0.1 PGPORT=55432 PGUSER=postgres
export PGPASSWORD=lab-only PGDATABASE=postgres

When pg_isready reports that the lab is ready, create the PGPM roles on that server, not on production:

bash
pgpm admin-users bootstrap

For a non-interactive shell:

bash
pgpm admin-users bootstrap --yes

Create the three migration files:

bash
cd packages/audit-demo
pgpm add create-documents

Save this as deploy/create-documents.sql. It grants reads on two empty tables; only one has RLS.

sql
CREATE SCHEMA app_public;
CREATE SCHEMA app_private;

CREATE TABLE app_public.documents (id integer PRIMARY KEY);
ALTER TABLE app_public.documents ENABLE ROW LEVEL SECURITY;
ALTER TABLE app_public.documents FORCE ROW LEVEL SECURITY;
CREATE POLICY documents_read ON app_public.documents
  FOR SELECT TO authenticated USING (id = 1);

CREATE TABLE app_private.import_queue (id integer PRIMARY KEY);

GRANT USAGE ON SCHEMA app_public, app_private TO authenticated;
GRANT SELECT ON app_public.documents, app_private.import_queue TO authenticated;

In revert/create-documents.sql, drop both tables, then their schemas. In verify/create-documents.sql, select id from each table with WHERE false.

Run the audit

From the workspace root, create the scaffold's initial performance baseline and run the first audit in report-only mode:

bash
cd ../..
pnpm run audit:db:baseline --report-only

The audit should show A2 (a grant on a table without RLS) and W1 (no exposure declared). safegres.config.js sets source: { pgpm: '.' }, so you don't need to deploy the module by hand.

Install the coding-agent skill

From your project root, run:

bash
npx skills add constructive-io/constructive --skill safegres

Choose project scope and your agent. The skill teaches the audit workflow; it doesn't install the CLI or set credentials. Review the skill source and commit its lockfile to let teammates restore the same revision with npx skills install.

Try: “Run Safegres doctor and a report-only audit on my lab. Explain the findings without changing the database or config.” If the skill and CLI disagree, check the installed CLI's --help.

Keep the lab running. Next, declare your PostgreSQL exposure.