Standalone PostgreSQL database setup and SQL runner with phase and directory modes
Project description
PG Runner: A Robust PostgreSQL Script Runner
PG Runner is a standalone, production-ready Python CLI tool for reliably executing SQL scripts against a PostgreSQL database. It’s built to handle the realities of non-trivial SQL files so your database setup and migrations are safe, repeatable, and easy to debug.
At its core is a robust SQL statement splitter that correctly handles semicolons appearing inside comments, string literals, and PostgreSQL dollar-quoted strings—preventing common script-running failures.
Key Features
-
Robust SQL parser:
-
Single-quoted strings (
'...') with escaped quotes ('') -
Double-quoted identifiers (
"...") with escaped quotes ("") -
Dollar-quoted strings (
$$...$$and$tag$...$tag$) -
Line comments (
-- ...) and block comments (/* ... */) -
Transactional by default:
-
Executes each file within a transaction; on error, changes are rolled back
-
Two execution modes:
- phases: For structured projects using named files (
schema.sql,indexes.sql,views.sql) - dir: Run all
.sqlfiles in a directory in alphabetical order
- phases: For structured projects using named files (
-
Non-transactional DDL support:
-
Autocommit mode for operations like
CREATE INDEX CONCURRENTLY -
Flexible configuration:
-
Use
--db-url, individual CLI flags (--host,--user, ...), orPOSTGRES_*environment variables
Installation
Use pip or pipx:
pip install pg-runner
# or
pipx install pg-runner
Requires Python 3.10+.
Usage
The pg-runner command provides two primary subcommands (phases and dir) and convenience commands for individual phases.
usage: pg-runner [-h] [--log-level LOG_LEVEL] [--no-emoji]
[--db-url DB_URL] [--host HOST] [--port PORT]
[--database DATABASE] [--user USER] [--password PASSWORD]
COMMAND ...
Standalone PostgreSQL database setup and SQL runner
options:
-h, --help show this help message and exit
--log-level LOG_LEVEL Logging level (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 all other options). Can also use DATABASE_URL or DB_URL env vars.
--host HOST DB host (env: POSTGRES_HOST; default: localhost)
--port PORT DB port (env: POSTGRES_PORT; default: 5432)
--database DATABASE DB name (env: POSTGRES_DB or POSTGRES_DATABASE; default: postgres)
--user USER DB user (env: POSTGRES_USER; default: postgres)
--password PASSWORD DB password (env: POSTGRES_PASSWORD; default: empty)
commands:
phases Run project-style phases (schema, indexes, views)
dir Run all .sql files in a directory alphabetically
schema Run only the 'schema' phase
indexes Run only the 'indexes' phase
views Run only the 'views' phase
Example: phases mode
Project-style layout:
my_project/
└── database/
├── schema.sql
├── indexes.sql
└── views.sql
Run all phases:
export POSTGRES_USER=myuser
export POSTGRES_PASSWORD=secret
export POSTGRES_DB=mydb
pg-runner phases --db-dir ./database
Run only a single phase:
pg-runner schema --db-dir ./database
Enable concurrent indexes (autocommit during indexes phase):
pg-runner phases --db-dir ./database --concurrent-indexes
Example: dir mode
Run every .sql file in alphabetical order:
pg-runner dir \
./migration_scripts \
--db-url "postgresql://myuser:secret@localhost:5432/mydb"
Recurse into subdirectories and run in autocommit mode:
pg-runner dir ./indexes --recursive --autocommit
Notes
- This tool is not a migration framework (no schema versioning or down migrations). It’s a robust SQL runner suitable for bootstrapping, seeding, and operational scripts.
- If your scripts create indexes concurrently, use
--concurrent-indexesin phases mode or--autocommitin dir mode to avoid running those statements inside a transaction.
License
MIT — 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pg_runner-0.1.3.tar.gz.
File metadata
- Download URL: pg_runner-0.1.3.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.7 Darwin/25.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8321857690800e38a03cb4874aec2310958af664f9dcb4855ac0565f6a01e94d
|
|
| MD5 |
e3da9559185e14f17da0bc8afea4065d
|
|
| BLAKE2b-256 |
56b270e912b1ffdd32f3aee329c063c96642ac26e945f7a01694f8a669a0048e
|
File details
Details for the file pg_runner-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pg_runner-0.1.3-py3-none-any.whl
- Upload date:
- Size: 10.4 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b8c33f82cb810a7bd0c4827f8568109b447bffd321aaa99ea99d83924d42a83
|
|
| MD5 |
997bb2068b0d904ca635ecd28f1cea6e
|
|
| BLAKE2b-256 |
8ac64ea007bcec1f560f7e065241ea731d899257701636c78acf3f2fda0060f1
|