Skip to main content

metapg.cli

Rich command-line interface for PostgreSQL operations

metapg.cli provides a beautiful terminal interface for managing PostgreSQL databases, migrations, and connection pools with rich formatting and intuitive commands.

Installation

pip install metapg.cli

This will install the metapg command globally.

Quick Start

# Migration commands
metapg migration status --db main
metapg migration apply --db main
metapg migration rollback --db main --steps 1
metapg migration create add_users_table --db main

# Pool management commands
metapg pool init mydb --dsn postgresql://localhost/mydb
metapg pool status mydb
metapg pool close mydb

# Backwards compatible (migration commands at root)
metapg status --db main
metapg apply --db main

Features

  • 🎨 Rich Interface - Beautiful tables, colors, and formatting
  • 📊 Migration Management - Status, apply, rollback, and create migrations
  • 🔗 Pool Management - Initialize, monitor, and close connection pools
  • ⚡ Fast Operations - Efficient database operations with progress indicators
  • 🛡️ Safe Defaults - Confirmation prompts for destructive operations
  • 🎛️ Multi-Database - Support for multiple named databases

Commands

Migration Commands

# Show migration status
metapg migration status [--db DATABASE] [--migrations-dir DIR]

# Apply pending migrations
metapg migration apply [--db DATABASE] [--target MIGRATION] [--force]

# Rollback migrations
metapg migration rollback [--db DATABASE] [--steps N]

# Create new migration
metapg migration create MIGRATION_NAME [--db DATABASE]

Pool Commands

# Initialize connection pool
metapg pool init [DATABASE] [--dsn CONNECTION_STRING] [--min-size N] [--max-size N]

# Show pool status
metapg pool status [DATABASE]

# Close connection pool
metapg pool close [DATABASE] [--force]

Global Options

  • --help - Show help message
  • --db DATABASE - Specify database name (default: "default")
  • --force - Skip confirmation prompts

Configuration

Environment Variables

  • DATABASE_URL - Default database connection string
  • DATABASE_URL_{NAME} - Connection string for named database

Migration Directory Structure

migrations/
├── default/           # Default database migrations
│   ├── 001_initial.sql
│   └── 002_add_users.sql
└── analytics/         # Named database migrations
    └── 001_events.sql

Migration File Format

-- Create users table
-- Created: 2024-01-01

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

-- ROLLBACK (optional)
DROP TABLE users;

Examples

Basic Migration Workflow

# Check current status
metapg migration status

# Create new migration
metapg migration create add_posts_table

# Edit the generated SQL file, then apply
metapg migration apply

# Check status again
metapg migration status

Multi-Database Setup

# Set up multiple databases
export DATABASE_URL="postgresql://localhost/main_db"
export DATABASE_URL_ANALYTICS="postgresql://localhost/analytics_db"

# Manage each database separately
metapg migration status --db default
metapg migration status --db analytics

metapg migration apply --db analytics

Pool Management

# Initialize pools for different databases
metapg pool init main --dsn postgresql://localhost/main_db
metapg pool init analytics --dsn postgresql://localhost/analytics_db

# Monitor pool status
metapg pool status main
metapg pool status analytics

# Close when done
metapg pool close main

Rich Output

The CLI provides beautiful, colored output with:

  • 📊 Status Tables - Clear migration status with timestamps
  • ✅ Success Indicators - Green checkmarks for completed operations
  • ⚠️ Warning Messages - Yellow alerts for pending operations
  • ❌ Error Messages - Red indicators for failed operations
  • 📈 Progress Bars - Visual progress for long operations

Integration

Programmatic Usage

import metapg.cli

# Access the CLI app directly
app = metapg.cli.app

# Use in scripts or other applications
if __name__ == "__main__":
    app()

CI/CD Integration

# GitHub Actions example
- name: Run migrations
  run: |
    metapg migration apply --force --db production
    metapg migration status --db production

License

MIT License - see LICENSE file for details.

Part of metapg

This package is part of the metapg metapackage for PostgreSQL operations.

Metadata

Release files for metapg.cli 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metapg.cli 0.0.2
File Size Uploaded Provenance
metapg_cli-0.0.2.tar.gz 30.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for metapg.cli 0.0.2
File Interpreter ABI Platform
metapg_cli-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 39.2 kB

Release files / metapg_cli-0.0.2.tar.gz

Download URL metapg_cli-0.0.2.tar.gz
Size 30.7 kB
Tags Source
SHA-256 checksum
How to use checksums
73bb4dd0681616e0dc2c53c42c3520ab6bc984aa43a887f9b3c906bc8321e517
BLAKE2b-256 checksum
How to use checksums
336ba9a84670ed17316222e2570f94cdb04ac39bbbfcbbfc7fdb9718bd1423ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.13.2 Darwin/23.5.0

Release files / metapg_cli-0.0.2-py3-none-any.whl

Download URL metapg_cli-0.0.2-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e178a65a73cdd168f64c1b990e4192eebba1706134cd16a66eacb283dee6a7e1
BLAKE2b-256 checksum
How to use checksums
3c6ceeff48f75c5cc748afdd72a5401c234a340e66d65a6a2d75f8ab66df3e18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.13.2 Darwin/23.5.0

Security for metapg.cli 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page