pg-cmd » Use cases » Data in and out of files
Download pg-cmd Free Trial » Buy pg-cmd Now »
The database is not the only place the data has to exist. A table needs to reach a spreadsheet or a warehouse; a file that arrived from somewhere has to become rows; an application wants JSON rather than a result grid. What is needed is a transfer that respects the details - the delimiter the receiving system expects, the quote character, whether the first line is a header - without writing a program to do it.
pg-cmd dsn="user:pass@host:5432/db" task=unload table=employees datafile=emp.csv delimiter=, header=y quit=y - CSV, TSV, TXT or JSON, from a table or from a query.task=load table=employees datafile=emp.csv filetype=csv delimiter=, quote=" header=y.
JSON is handled in the same way, in both directions, which is what makes the tool useful in a pipeline rather than only at a desk: a file of JSON records can be loaded into a table, and a table can be written out as JSON for whatever reads it next. The guide covers the file-type specific options.
The modes: import from a file, export to a file, import JSON, export JSON. Running it unattended: shell scripts, batch mode. Working interactively: interactive SQL.