Skip to main content

PostgreSQL query and administration CLI tool

Project description

sql-tool

PostgreSQL and TimescaleDB query and administration CLI tool.

Provides multiple output formats (table, JSON, CSV), flexible connection management (CLI flags, DSN, PG* env vars, config profiles), and admin commands for database inspection and maintenance.

Installation

git clone <repository-url>
cd sql-tool
uv sync

Run commands with uv run sql-tool or install globally:

uv tool install .

Quick Start

# Check connectivity
sql-tool service check

# Run a query
sql-tool query -e "SELECT now()"

# List databases
sql-tool databases

# List tables in a schema
sql-tool -s myschema table

# Show table columns and preview data
sql-tool table myschema.sensor_data --head 5

Commands

Query Execution

# Inline query
sql-tool query -e "SELECT * FROM users LIMIT 10"

# From file
sql-tool query queries/report.sql

# From stdin
echo "SELECT 1" | sql-tool query

# With timeout
sql-tool query -e "SELECT pg_sleep(5)" -t 3

Database Overview

# List all databases (sorted by size)
sql-tool databases

# List schemas with space usage
sql-tool schema

# Schemas across all databases
sql-tool schema --all-databases

Table Inspection

# List all tables with sizes
sql-tool table

# Filter by schema
sql-tool -s myschema table

# Show column definitions
sql-tool table myschema.sensor_data

# Preview data
sql-tool table myschema.sensor_data --head 10
sql-tool table myschema.sensor_data --tail 5
sql-tool table myschema.sensor_data --sample 3

# Show timestamp range for hypertables
sql-tool table myschema.sensor_data --range

Connections

# Active connections (non-idle)
sql-tool connections

# All connections including idle
sql-tool connections --all

# Connection summary with memory config
sql-tool connections --summary

# Filter by user, database, or state
sql-tool connections --filter-user appuser --filter-db mydb
sql-tool connections --min-duration 5

Service & Maintenance

# Check server connectivity and version
sql-tool service check

# Vacuum a table
sql-tool service vacuum my_table
sql-tool service vacuum my_table --full
sql-tool service vacuum --all

# Kill a backend process
sql-tool service kill 12345
sql-tool service kill 12345 --cancel  # cancel query only

TimescaleDB

# List hypertables
sql-tool ts hypertables

# Show chunks for a hypertable
sql-tool ts chunks myschema.sensor_data

# Compression statistics
sql-tool ts compression
sql-tool ts compression myschema.sensor_data

# Compression settings and policies
sql-tool ts compression-settings -s myschema

# Configure compression
sql-tool ts compression-set myschema.sensor_data --segmentby "device_id, sensor" --orderby "timestamp DESC"
sql-tool ts compression-set myschema.sensor_data --policy "4 hours"

# Compress chunks
sql-tool ts compress myschema.sensor_data
sql-tool ts compress myschema.sensor_data --chunk 11420

# Continuous aggregates
sql-tool ts caggs

# Retention policies
sql-tool ts retention

# Background jobs
sql-tool ts jobs
sql-tool ts jobs --history
sql-tool ts jobs --history --job 1005

Configuration

# Show resolved config with source attribution
sql-tool config show

# List available profiles
sql-tool config profiles

Output Formats

All listing commands support multiple output formats:

# Table format (default in terminal)
sql-tool -f table databases

# JSON format
sql-tool -f json databases

# Compact JSON
sql-tool --compact -f json databases

# CSV format (default when piped)
sql-tool databases | head

# CSV without header
sql-tool --no-header databases

Auto-detection: table format for TTY, CSV when piped.

Connection Options

Global flags (must appear before the subcommand):

sql-tool -H myhost -p 5432 -d mydb -U myuser query -e "SELECT 1"
sql-tool --dsn "postgresql://user@host/db" databases
sql-tool -P production table
Flag Env Var Description
-H, --host PGHOST PostgreSQL host
-p, --port PGPORT PostgreSQL port
-d, --database PGDATABASE Database name
-U, --user PGUSER User name
-W, --password PGPASSWORD Password
--dsn Connection DSN
-P, --profile SQL_PROFILE Named profile

Configuration File

Location: ~/.config/sql-tool/config.toml

default_timeout = 30
default_format = "table"

[profiles.local]
host = "localhost"
port = 5432
dbname = "postgres"
user = "postgres"

[profiles.production]
dsn = "postgresql://readonly@prod-db.example.com:5432/myapp?sslmode=verify-full"

[profiles.staging]
dsn = "postgresql://user@staging-db.example.com/myapp"
sslmode = "verify-full"
connect_timeout = 5

See config.example.toml for a full example.

Precedence (highest to lowest)

  1. CLI flags (--host, --port, etc.)
  2. --dsn flag
  3. Environment variables (PGHOST, PGPORT, etc.)
  4. Named profile (--profile or SQL_PROFILE)
  5. Config file defaults
  6. Built-in defaults (localhost:5432/postgres)

Exit Codes

Code Meaning
0 Success
1 General error (SQL syntax, permission denied)
2 Usage error (invalid arguments)
3 Input error (file not found, invalid parameters)
5 Network error (connection failed, authentication)
6 Timeout (query timeout, connection timeout)
7 Configuration error (malformed config, unknown profile)

Development

# Run tests
uv run pytest

# Run with coverage
uv run pytest --cov=sql_tool

# Lint
uv run ruff check src/

# Type check
uv run mypy src/

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

tsdb_tool-0.1.0.tar.gz (226.6 kB view details)

Uploaded Source

Built Distribution

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

tsdb_tool-0.1.0-py3-none-any.whl (42.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tsdb_tool-0.1.0.tar.gz
  • Upload date:
  • Size: 226.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for tsdb_tool-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a378348a852e7d4aa5328414e484f76a4b3981ce329710e23b15d5a22c60fbe2
MD5 d5023cdb429d4cea6c9ac6784fecbb95
BLAKE2b-256 dfa6c107b592ec1cbd0668d627670e2dc0d5b1ab42c07215bb6c1131b58efef3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tsdb_tool-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for tsdb_tool-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e40056817c282349ab2ba90ff0111b94bcdcc4670efc2c646ba1d3ab9781f41c
MD5 247c3afc3c2d868253a05f9ca95c0320
BLAKE2b-256 be78334a5b7cea64dcdbc5d56f55b7eebc73335eaa8e411017ab533e7491c990

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