Skip to main content

Extract PostgreSQL records with all related data via FK relationships

Project description

PgSlice Logo

pgslice

Codecov PyPI PyPI - Wheel PyPI - Python Version PyPI - License

Python CLI tool for extracting PostgreSQL records with all related data via foreign key relationships.

Overview

pgslice extracts a specific database record and ALL its related records by following foreign key relationships bidirectionally. Perfect for:

  • Reproducing production bugs locally with real data
  • Creating partial database dumps for specific users/entities
  • Testing with realistic data subsets
  • Debugging issues that only occur with specific data states

Extract only what you need while maintaining referential integrity.

Features

  • Bidirectional FK traversal: Follows relationships in both directions (forward and reverse)
  • Circular relationship handling: Prevents infinite loops with visited tracking
  • Multiple records: Extract multiple records in one operation
  • Timeframe filtering: Filter specific tables by date ranges
  • PK remapping: Auto-remaps auto-generated primary keys for clean imports
  • Interactive REPL: User-friendly command-line interface
  • Schema caching: SQLite-based caching for improved performance
  • Type-safe: Full type hints with mypy strict mode
  • Secure: SQL injection prevention, secure password handling

Installation

From PyPI (Recommended)

# Install with pipx (isolated environment, recommended)
pipx install pgslice

# Or with pip
pip install pgslice

# Or with uv
uv tool install pgslice

From Docker Hub

# Pull the image
docker pull <your-dockerhub-username>/pgslice:latest

# Run pgslice
docker run --rm -it \
  -v $(pwd)/dumps:/home/pgslice/.pgslice/dumps \
  -e PGPASSWORD=your_password \
  <your-dockerhub-username>/pgslice:latest \
  pgslice --host your.db.host --port 5432 --user your_user --database your_db

From Source (Development)

See DEVELOPMENT.md for detailed development setup instructions.

Quick Start

# In REPL:
# This will dump all related records to the film with id 1
# The generated SQL file will be placed, by default, in ~/.pgslice/dumps
# The name will be a formated string with table name, id, and timestamp
pgslice> dump "film" 1

# You can overwrite the output path with:
pgslice> dump "film" 1 --output film_1.sql

# Extract multiple records
pgslice> dump "actor" 1,2,3 --output multiple_actors.sql

# Use wide mode to follow all relationships (including self-referencing FKs)
# Be cautions that this can result in larger datasets. So use with caution
pgslice> dump "customer" 42 --wide --output customer_42.sql

# Apply timeframe filter
pgslice> dump "customer" 42 --timeframe "rental:rental_date:2024-01-01:2024-12-31"

# List all tables
pgslice> tables

# Show table structure and relationships
pgslice> describe "film"

# Keep original primary key values (no remapping)
# By default, we will dinamically assign ids to the new generated records
# and handle conflicts gracefully. Meaninh, you can run the same file multiple times
# and no conflicts will arise.
# If you want to keep the original id's run:
pgslice> dump "film" 1 --keep-pks --output film_1.sql

Configuration

Key environment variables (see .env.example for full reference):

Variable Description Default
DB_HOST Database host localhost
DB_PORT Database port 5432
DB_NAME Database name -
DB_USER Database user -
DB_SCHEMA Schema to use public
PGPASSWORD Database password (env var only) -
CACHE_ENABLED Enable schema caching true
CACHE_TTL_HOURS Cache time-to-live 24
LOG_LEVEL Logging level INFO
PGSLICE_OUTPUT_DIR Output directory ~/.pgslice/dumps

Security

  • Parameterized queries: All SQL uses proper parameterization
  • SQL injection prevention: Identifier validation
  • Secure passwords: Never logged or stored
  • Read-only enforcement: Safe for production databases

Contributing

Contributions are welcome! See DEVELOPMENT.md for comprehensive development documentation including:

  • Local development setup
  • Code quality standards and testing guidelines
  • Version management and publishing workflow
  • Architecture and design patterns

Quick start for contributors:

make setup        # One-time setup (installs dependencies, hooks)
make test         # Run all tests
git commit        # Pre-commit hooks run automatically (linting, formatting, type-checking)

For troubleshooting common development issues, see the Troubleshooting section in DEVELOPMENT.md.

License

MIT

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

pgslice-0.1.1.tar.gz (37.9 kB view details)

Uploaded Source

Built Distribution

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

pgslice-0.1.1-py3-none-any.whl (40.9 kB view details)

Uploaded Python 3

File details

Details for the file pgslice-0.1.1.tar.gz.

File metadata

  • Download URL: pgslice-0.1.1.tar.gz
  • Upload date:
  • Size: 37.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pgslice-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2c9744c461c4cdda94fe448d3742ca4b1e3fb2f3a733d619c457d9be7cd26fd5
MD5 793b2a834b801cea8471dfa8925a0a2f
BLAKE2b-256 029f6c72cb705aa5266630f495d6bff48bea7c882b9b32a51b78c7da80fba9cb

See more details on using hashes here.

File details

Details for the file pgslice-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pgslice-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 40.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pgslice-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c0aa80691e9641c9469d48fad9bd32b215b068cf5e575cfbcdbe48569b8a0c30
MD5 26c2fe28bea47cdfb9802b7f9b64abc7
BLAKE2b-256 95dd699e84efa3c74f548c8d0173a0cae2b4a5fe970d407622dc8298257507bd

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