Data Import/Export
Data Import/Export guide for pgcli
Export and import databases to dump files. Supports custom format (recommended) and plain SQL, with automatic gzip compression. Also supports piping between instances.
Format comparison:
| Feature | Custom (.dump) |
SQL (.sql) |
|---|---|---|
| Import speed | Faster (binary format) | Slower (text format) |
| File size | Smaller (compressed) | Larger (plain text) |
| Human-readable | No | Yes |
| Selective restore | Yes (specific tables) | No |
| Best for | Migration, backup, large databases | Version control, CI seed data, manual editing |
Format detection: Uses magic bytes (content-based) with extension fallback.
- Files starting with
PGDMP→ custom format .sqlor.sql.gz→ plain SQL format.gzextension or gzip magic bytes (0x1f 0x8b) → automatic decompression- Extension is used as fallback if content detection fails
Remote databases (–dsn): Connect to any PostgreSQL instance using a connection string.
- No local PostgreSQL installation needed
- Works with local-to-remote, remote-to-local, and remote-to-remote migrations
- Supports all the same flags as local instances (
-o,-d,--clean,-v,--compress)
Note on existing data: Importing into a database with existing tables will fail unless you use the --clean flag, which drops objects before restoring. Use --clean when importing into a database that already contains data.
Use cases:
- Migrate data between instances:
pg export -i proj01 | pg import -i proj02 - Cross-host migration:
pg export -i proj01 | pg import --dsn postgres://user:pass@remote:5432/db - Share database with team:
pg export -i proj01 -o dump.dump.gz(compressed, smaller file) - Backup before major changes:
pg export -i proj01 -o pre-migration.sql.gz - CI/CD pipelines: export test data, import into fresh test databases