From aa2ba50c2c13dd35a95a91638aa0a2b88c6df1eb Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 26 Nov 2018 15:18:55 -0500 Subject: Add CSV table output mode in psql. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "\pset format csv", or --csv, selects comma-separated values table format. This is compliant with RFC 4180, except that we aren't too picky about whether the record separator is LF or CRLF; also, the user may choose a field separator other than comma. This output format is directly compatible with the server's COPY CSV format, and will also be useful as input to other programs. It's considerably safer for that purpose than the old recommendation to use "unaligned" format, since the latter couldn't handle data containing the field separator character. Daniel Vérité, reviewed by Fabien Coelho and David Fetter, some tweaking by me Discussion: https://postgr.es/m/a8de371e-006f-4f92-ab72-2bbe3ee78f03@manitou-mail.org --- doc/src/sgml/ref/psql-ref.sgml | 98 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 19 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 6e6d0f42d13..47714eb0c8c 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -68,8 +68,8 @@ PostgreSQL documentation Switches to unaligned output mode. (The default output mode is - otherwise aligned.) This is equivalent to \pset format - unaligned. + aligned.) This is equivalent to + \pset format unaligned. @@ -151,6 +151,16 @@ EOF + + + + + Switches to CSV (Comma-Separated Values) output + mode. This is equivalent to \pset format csv. + + + + @@ -270,8 +280,8 @@ EOF - Turn on HTML tabular output. This is - equivalent to \pset format html or the + Switches to HTML output mode. This is + equivalent to \pset format html or the \H command. @@ -2520,6 +2530,19 @@ lo_import 152801 + + csv_fieldsep + + + Specifies the field separator to be used in + CSV output format. If the separator character + appears in a field's value, that field is output within double + quotes, following standard CSV rules. + The default is a comma. + + + + expanded (or x) @@ -2547,8 +2570,8 @@ lo_import 152801 Specifies the field separator to be used in unaligned output - format. That way one can create, for example, tab- or - comma-separated output, which other programs might prefer. To + format. That way one can create, for example, tab-separated + output, which other programs might prefer. To set a tab as field separator, type \pset fieldsep '\t'. The default field separator is '|' (a vertical bar). @@ -2585,22 +2608,48 @@ lo_import 152801 Sets the output format to one of aligned, - asciidoc, html, - latex (uses tabular), + asciidoc, + csv, + html, + latex, latex-longtable, troff-ms, unaligned, or wrapped. Unique abbreviations are allowed. + aligned format is the standard, + human-readable, nicely formatted text output; this is the default. + + unaligned format writes all columns of a row on one line, separated by the currently active field separator. This is useful for creating output that might be intended to be read - in by other programs (for example, tab-separated or comma-separated - format). + in by other programs, for example, tab-separated or comma-separated + format. However, the field separator character is not treated + specially if it appears in a column's value; + so CSV format may be better suited for such + purposes. - aligned format is the standard, human-readable, - nicely formatted text output; this is the default. + csv format + + CSV (Comma-Separated Values) format + in psql + + writes column values separated by commas, applying the quoting + rules described in + RFC 4180. + This output is compatible with the CSV format of the server's + COPY command. + A header line with column names is generated unless + the tuples_only parameter is + on. Titles and footers are not printed. + Each row is terminated by the system-dependent end-of-line character, + which is typically a single newline (\n) for + Unix-like systems or a carriage return and newline sequence + (\r\n) for Microsoft Windows. + Field separator characters other than comma can be selected with + \pset csv_fieldsep. wrapped format is like aligned but wraps @@ -2620,8 +2669,12 @@ lo_import 152801 language. They are not complete documents! This might not be necessary in HTML, but in LaTeX you must have a complete - document wrapper. latex-longtable - also requires the LaTeX + document wrapper. + The latex format + uses LaTeX's tabular + environment. + The latex-longtable format + requires the LaTeX longtable and booktabs packages. @@ -4588,17 +4641,24 @@ first second peter@localhost testdb=> \pset border 1 Border style is 1. -peter@localhost testdb=> \pset format unaligned -Output format is unaligned. -peter@localhost testdb=> \pset fieldsep "," -Field separator is ",". +peter@localhost testdb=> \pset format csv +Output format is csv. peter@localhost testdb=> \pset tuples_only -Showing only tuples. +Tuples only is on. peter@localhost testdb=> SELECT second, first FROM my_table; one,1 two,2 three,3 four,4 +peter@localhost testdb=> \pset format unaligned +Output format is unaligned. +peter@localhost testdb=> \pset fieldsep '\t' +Field separator is " ". +peter@localhost testdb=> SELECT second, first FROM my_table; +one 1 +two 2 +three 3 +four 4 Alternatively, use the short commands: -- cgit v1.2.3