Skip to main content

Standalone PostgreSQL database setup and SQL runner with phase and directory modes

Project description

PG Runner: A Robust PostgreSQL Script Runner

Python 3.13+ License: MIT

PG Runner is a standalone, production-ready Python CLI tool for reliably executing SQL scripts against a PostgreSQL database. It's designed to handle the complexities of real-world SQL files, making database setups and migrations safe, repeatable, and easy to debug.

The core of this tool is a sophisticated SQL statement parser that correctly handles semicolons inside comments, string literals, and PostgreSQL-specific dollar-quoted strings, preventing common script-running failures.

Key Features

  • 🛡️ Robust SQL Parser: Intelligently splits SQL files into individual statements, correctly handling:
    • Single-quoted strings ('...') with escapes ('').
    • Double-quoted identifiers ("...") with escapes ("").
    • PostgreSQL dollar-quoted strings ($$...$$ and $tag$...$tag$).
    • Line (--) and block (/* ... */) comments.
  • ⚙️ Transactional Safety: Executes each SQL file within a single transaction by default. If any statement fails, the entire batch is rolled back, ensuring your database remains in a consistent state.
  • 🚀 Two Execution Modes:
    1. phases mode: For structured projects, executes specific, named SQL files (schema.sql, indexes.sql, etc.) in a defined order.
    2. dir mode: A general-purpose mode that finds and executes all .sql files in a directory alphabetically.
  • ⚡ Non-Transactional DDL: Supports autocommit mode for operations that cannot run inside a transaction, such as CREATE INDEX CONCURRENTLY.
  • 🔌 Flexible Configuration: Configure your database connection via a single --db-url, discrete CLI flags (--host, --user, etc.), or standard POSTGRES_* environment variables.

Installation

Clone the repository and install it using pip. This will make the pg-runner command available in your shell.

git clone [https://github.com/khodaparastan/pg-runner.git](https://github.com/khodaparastan/pg-runner.git)
cd pg-runner
pip install .

Usage

The tool is invoked via the pg-runner command, which has two main subcommands: phases and dir.

$ pg-runner --help
usage: pg-runner [-h] [--log-level LOG_LEVEL] [--no-emoji] --db-url DB_URL | --host HOST | ... {phases,dir,...} ...

Standalone PostgreSQL database setup and SQL runner

options:
  -h, --help            show this help message and exit
  --log-level LOG_LEVEL
                        Logging level (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL). Default: INFO
  --no-emoji            Disable emoji output for CI environments

database connection options:
  --db-url DB_URL       SQLAlchemy DB URL (overrides other connection options). Can also be provided via DATABASE_URL or DB_URL
  --host HOST           DB host (default: POSTGRES_HOST or localhost)
  --port PORT           DB port (default: POSTGRES_PORT or 5432)
  --database DATABASE   Database name (default: POSTGRES_DB/POSTGRES_DATABASE or postgres)
  --user USER           DB user (default: POSTGRES_USER or postgres)
  --password PASSWORD   DB password (default: POSTGRES_PASSWORD or empty)

subcommands:
  {phases,dir,schema,indexes,views,price_analysis_views}
    phases              Run project-style phases (schema, indexes, views, price_analysis_views)
    dir                 Run all .sql files in a directory in alphabetical order
    schema              Run only the schema phase
    indexes             Run only the indexes phase
    views               Run only the views phase

Example 1: phases mode

This mode is ideal for projects with a defined structure.

Directory Structure:

my_project/
└── database/
    ├── schema.sql
    ├── indexes.sql
    └── views.sql

Command:

# Set environment variables for the connection
export POSTGRES_USER=myuser
export POSTGRES_PASSWORD=secret
export POSTGRES_DB=mydb

# Run all phases in the default order
pg-runner phases --db-dir ./database

# Run only the 'schema' phase using the convenience command
pg-runner schema --db-dir ./database

Example 2: dir mode

This mode is for running a collection of SQL scripts in a generic directory.

Directory Structure:

migration_scripts/
├── 01_create_users.sql
├── 02_create_products.sql
└── 03_populate_data.sql

Command:

# Run all .sql files in the directory using a DB URL
pg-runner dir \
  --sql-dir ./migration_scripts \
  --db-url "postgresql://myuser:secret@localhost/mydb"

To run with non-transactional execution (e.g., for concurrent index creation), use --autocommit:

pg-runner dir --sql-dir ./indexes --autocommit

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg_runner-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_runner-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file pg_runner-0.1.0.tar.gz.

File metadata

  • Download URL: pg_runner-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.7 Darwin/25.0.0

File hashes

Hashes for pg_runner-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6500df06b6fa084785acf502d435ad373fbef79732d478cf4ad8261ee7b6d2e6
MD5 fa750fe08723c94facf67e518343819b
BLAKE2b-256 660bc2e89e0c7e0ecf5e6101cefb6414fb01cff64dd79c71aa90f9d8814f83f2

See more details on using hashes here.

File details

Details for the file pg_runner-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pg_runner-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.7 Darwin/25.0.0

File hashes

Hashes for pg_runner-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de7194d5e6d699ca94ac23ac592394b5498e0be83380eae0ba5e42a9f0a193ef
MD5 550005bc9936e1769de02f549a46a0f4
BLAKE2b-256 0c16cdd168e4428a3f2ec6ee68bf68f86c4f3a3b7d032ddbee219c6fbc3e68d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page