"\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
aa2ba50c2c Add CSV table output mode in psql.
doc/src/sgml/ref/psql-ref.sgml | 98 +++++++++++++++++++++------
src/bin/psql/command.c | 39 ++++++++++-
src/bin/psql/help.c | 16 +++--
src/bin/psql/settings.h | 1 +
src/bin/psql/startup.c | 7 ++
src/bin/psql/tab-complete.c | 5 +-
src/fe_utils/print.c | 131 ++++++++++++++++++++++++++++++++++++-
src/include/fe_utils/print.h | 2 +
src/test/regress/expected/psql.out | 89 +++++++++++++++++++++++++
src/test/regress/sql/psql.sql | 48 ++++++++++++++
10 files changed, 402 insertions(+), 34 deletions(-)