Clone this repo and run:
make && make install
To install the extension:
create extension pg_csv;
Aggregate that builds a CSV as per RFC 4180, quoting as required.
select csv_agg(x) from projects x;
csv_agg
-------------------
id,name,client_id+
1,Windows 7,1 +
2,Windows 10,1 +
3,IOS,2 +
4,OSX,2 +
5,Orphan,
(1 row)
You can use a custom delimiter.
select csv_agg(x, csv_options(delimiter := '|')) from projects x;
csv_agg
-------------------
id|name|client_id+
1|Windows 7|1 +
2|Windows 10|1 +
3|IOS|2 +
4|OSX|2 +
5|Orphan|
(1 row)
Note
Newline, carriage return and double quotes are not supported as delimiters to maintain the integrity of the separated values format.
You can include a byte-order mark (BOM) to make the CSV compatible with Excel.
select csv_agg(x, csv_options(bom := true)) from projects x;
csv_agg
-------------------
id,name,client_id+
1,Windows 7,1 +
2,Windows 10,1 +
3,IOS,2 +
4,OSX,2 +
5,Orphan,
(1 row)
You can omit or include the CSV header.
select csv_agg(x, csv_options(header := false)) from projects x;
csv_agg
-------------------
1,Windows 7,1 +
2,Windows 10,1 +
3,IOS,2 +
4,OSX,2 +
5,Orphan,
(1 row)