PostgreSQL SQL Test Runner

1.0.7 · active · verified Sun Apr 19

pg-test is a focused command-line interface (CLI) utility designed for running tests directly from `.sql` files against a PostgreSQL database. As of version 1.0.7, it provides a straightforward mechanism for database-centric testing, executing each `.sql` file in a specified directory sequentially and stopping on the first failure. Its release cadence appears to be stable and utility-focused, without frequent major updates. Key differentiators include its simplicity, direct execution of SQL scripts, and reliance on a `DB` environment variable for database connection, making it suitable for CI environments like Travis-CI. Unlike typical JavaScript test runners, pg-test primarily operates on raw SQL files rather than integrating with JS/TS test frameworks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to run `pg-test` programmatically using `child_process` after setting the required `DB` environment variable, including creating a temporary SQL test file.

import { spawnSync } from 'child_process';
import path from 'path';
import fs from 'fs';

// Create a dummy test file
const testFilePath = path.join(process.cwd(), 'temp_test.sql');
fs.writeFileSync(testFilePath, 'SELECT 1 = 1; -- Should pass');

// Set the DB environment variable (replace with your actual DB URL)
// For local PostgreSQL without password: 'postgres://postgres@localhost/your_db_name'
// Make sure 'your_db_name' exists and is accessible.
process.env.DB = process.env.DB ?? 'postgres://postgres@localhost/testdb';

console.log(`Running pg-test against: ${process.env.DB}`);

const result = spawnSync('pg-test', [path.dirname(testFilePath)], {
  stdio: 'inherit',
  env: process.env
});

if (result.status !== 0) {
  console.error('pg-test failed!');
} else {
  console.log('pg-test completed successfully.');
}

// Clean up the dummy test file
fs.unlinkSync(testFilePath);

view raw JSON →