pgsql-test Brings Real PostgreSQL Databases Into Application Test Loops

pgsql-test Brings Real PostgreSQL Databases Into Application Test Loops

AI-generated editorial illustration; not a documentary photograph.

A PostgreSQL project article presents pgsql-test, an MIT-licensed testing harness that runs an ephemeral PostgreSQL database inside an application's existing test runner. It seeds the database once, rolls each test back to that state, and lets developers test database behavior alongside JavaScript, TypeScript or Python code.

Why Mocks Fall Short

Mocks can show how application code handles an expected result, but they do not execute PostgreSQL's foreign keys, triggers, functions, constraints or row-level security policies. That gap can push business rules into application code even when the database is the more appropriate enforcement point.

Testing Access Policies

The harness includes an administrative client for setup and an application client for testing grants and policies. Its setContext() mechanism applies database roles and identity settings within the current transaction, allowing tests to exercise the permissions used by the application rather than a superuser that would bypass row-level security.

In the article's example, a query without an ownership filter must return Alice's document while excluding Bob's. The test supplies the authenticated role and user identity expected by the policy; pgsql-test does not validate an authentication token.

Ecosystem Integrations

Seeding is handled through pgsql-seed, which supports SQL files, programmatic fixtures, CSV, JSON and migrations. Related integrations cover Supabase, Drizzle ORM, in-process PGlite, PostGraphile and GraphQL subscriptions. The projects and integrations are described as MIT licensed and available through npm and PyPI.

From Local Tests To CI

The source also describes pgpm scaffolding workspaces with pgsql-test, Jest and GitHub Actions. In that setup, deployment, verification and rollback scripts accompany schema changes. A separate CI tool, safegres, checks deployed schemas for security and performance regressions against a committed baseline, extending database behavior checks beyond the test runner.

Source: pgsql-test: Real Postgres Testing for Faster Development Loops

Share this article
FacebookThreadsLinkedInX

Leave a Reply

Your email address will not be published. Required fields are marked *