pg-export
Overview
CLI tool for exporting PostgreSQL data to timestamped CSV files. Supports four modes: list tables, export single table, export all tables, or execute a custom query. Works against any PostgreSQL 12+ instance with auto-schema discovery.
Usage
pg-export <MODE> [OPTIONS]
Modes
Exactly one mode must be specified per invocation.
| Mode | Description |
|---|---|
-l, --list-tables | List all tables and exit |
-t, --table NAME | Export a single table to CSV |
-a, --all | Export all tables (one CSV per table) |
-q, --query SQL | Execute custom SQL and export to CSV |
Options
| Option | Description | Default |
|---|---|---|
-c, --connection-string URL | PostgreSQL connection string | DATABASE_URL env var |
-o, --output DIR | Output directory | ./exports |
-f, --filename NAME | Override auto-generated filename | -- |
-w, --where CLAUSE | SQL WHERE filter (--table and --all only) | -- |
Prerequisites
- Repo: pg-export
- Install:
npm installfrom repo root - Environment:
DATABASE_URLor--connection-stringflag - Database: PostgreSQL 12+
Examples
List all tables
pg-export --list-tables
Export a single table
pg-export --table=contact --output=./exports
Export with a WHERE filter
pg-export --table=contact --where="location_id = 59" --output=./exports
Export all tables
pg-export --all --output=./exports
Custom SQL query
pg-export --query="SELECT c.id, c.name FROM contact c WHERE c.active = true" \
--filename=active-contacts.csv --output=./exports
Use explicit connection string
pg-export --connection-string "postgresql://user:pass@host:5432/mydb" --list-tables
Output
- Format: CSV with headers (UTF-8)
- Naming:
{table}-YYYY-MM-DD.csvfor tables,query-YYYY-MM-DD.csvfor queries - Empty tables: 0-byte file written
- Credentials: Automatically redacted in console output