Skip to main content

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.

ModeDescription
-l, --list-tablesList all tables and exit
-t, --table NAMEExport a single table to CSV
-a, --allExport all tables (one CSV per table)
-q, --query SQLExecute custom SQL and export to CSV

Options

OptionDescriptionDefault
-c, --connection-string URLPostgreSQL connection stringDATABASE_URL env var
-o, --output DIROutput directory./exports
-f, --filename NAMEOverride auto-generated filename--
-w, --where CLAUSESQL WHERE filter (--table and --all only)--

Prerequisites

  • Repo: pg-export
  • Install: npm install from repo root
  • Environment: DATABASE_URL or --connection-string flag
  • 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.csv for tables, query-YYYY-MM-DD.csv for queries
  • Empty tables: 0-byte file written
  • Credentials: Automatically redacted in console output