This program converts a PostgreSQL dump file to a CSV file. It assumes that the dump file stores its data in a COPY block.
Below is an example of a COPY block in a PostgreSQL dump file (data excerpted from Spice OSS):
COPY public.cleaned_sales_data (order_number, quantity_ordered, price_each, order_line_number, sales, order_date, status, quarter, month, year, product_line, msrp, product_code, customer_name, phone, address_line1, address_line2, city, state, postal_code, country, territory, contact_last_name, contact_first_name, deal_size) FROM stdin;
10107 30 95.7 2 2871 2003-02-24 00:00:00 Shipped 1 2 2003 Motorcycles 95 S10_1678 Land of Toys Inc. 2125557818 897 Long Airport Avenue \N NYC NY 10022 USA \N Yu Kwai Small
10121 34 81.35 5 2765.9 2003-05-07 00:00:00 Shipped 2 5 2003 Motorcycles 95 S10_1678 Reims Collectables 26.47.1555 59 rue de l'Abbaye \N Reims \N 51100 France EMEA Henriot Paul Small
\.Output CSV file:
10107,30,95.7,2,2871,2003-02-24 00:00:00,Shipped,1,2,2003,Motorcycles,95,S10_1678,Land of Toys Inc.,2125557818,897 Long Airport Avenue,,NYC,NY,10022,USA,,Yu,Kwai,Small
10121,34,81.35,5,2765.9,2003-05-07 00:00:00,Shipped,2,5,2003,Motorcycles,95,S10_1678,Reims Collectables,26.47.1555,59 rue de l'Abbaye,,Reims,,51100,France,EMEA,Henriot,Paul,SmallWith header:
order_number,quantity_ordered,price_each,order_line_number,sales,order_date,status,quarter,month,year,product_line,msrp,product_code,customer_name,phone,address_line1,address_line2,city,state,postal_code,country,territory,contact_last_name,contact_first_name,deal_size
10107,30,95.7,2,2871,2003-02-24 00:00:00,Shipped,1,2,2003,Motorcycles,95,S10_1678,Land of Toys Inc.,2125557818,897 Long Airport Avenue,,NYC,NY,10022,USA,,Yu,Kwai,Small
10121,34,81.35,5,2765.9,2003-05-07 00:00:00,Shipped,2,5,2003,Motorcycles,95,S10_1678,Reims Collectables,26.47.1555,59 rue de l'Abbaye,,Reims,,51100,France,EMEA,Henriot,Paul,Smallcd postgres_to_csv
cargo run -- [OPTIONS] <INPUT FILE PATH> <OUTPUT FILE PATH>Options:
-a: Add header row to CSV file, which includes the names of each column.-i <string>: Change delimiter of input file from default (tab).-o <string>: Change delimiter of output file from default (,).
Note:
- To pass tab as a command line argument, type
$'\t'. - To pass space as a command line argument, type
' '.