Lint PostgreSQL Migrations with pgsql-lint
Dan Lynch
@pgsql/lint checks SQL and PL/pgSQL function definitions in migration files. It parses the source into an abstract syntax tree (AST) without executing it. You don't need a database or credentials.
Run a built-in rule
Use Node.js 22 or later. From an empty lab directory:
bashnpm init -y npm install --save-dev @pgsql/lint mkdir -p migrations
Save this in migrations/001.sql:
sqlCREATE FUNCTION app_public.document_ids() RETURNS SETOF integer LANGUAGE sql AS $$ SELECT id FROM documents; $$;
Run C3, which requires schema-qualified relation references inside functions:
bashnpx --no-install pgsql-lint --no-config --rules C3 migrations
The CLI reports C3 and exits with status 1. Change FROM documents to FROM app_public.documents and rerun it. The check now exits with status 0, even though the table hasn't been created: the linter checks the spelling of the reference, not whether it resolves.
Keep this migration for the next lesson. Use Safegres separately to audit deployed grants, row-level security, and exposure. safegres lint is a live audit, not the pgsql-lint file command.
Next, write a custom rule.

