This page describes various methods for extracting data from a database into a plain text format (e.g. CSV, JSON, XML etc) ingesting data into a database from a plain text format and manipulating data in a plain text file from within the database. The Database technologies where each method applies are shown by the buttons in the right hand column.
Tab separated, newline terminated...
SELECT mycol1,mycol2,mycol3
  FROM mytable
  INTO OUTFILE '/tmp/myfile.txt';
Comma separated, newline terminated, each field enclosed in double quotes...
SELECT mycol1,mycol2,mycol3
  FROM mytable
  INTO OUTFILE '/tmp/myfile.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
SET MARKUP csv ON DELIMITER ,
SPOOL myfile.csv
SELECT mycol1,mycol2,mycol3
  FROM mytable;
SPOOL OFF
SET COLSEP ,
SET PAGESIZE 0
SET TRIMSPOOL ON
SET HEADSEP OFF
SET LINESIZE 80
SET NUMW 10
SPOOL myfile.csv
SELECT mycol1,mycol2,mycol3
  FROM mytable;
SPOOL OFF
In psql...
SELECT mycol1,mycol2,mycol3
FROM mytable \g /tmp/myfile.txt
To append to an existing file...
SELECT mycol1,mycol2,mycol3
FROM mytable \g | cat >>/tmp/myfile.txt
Alternate method in psql...
\o /tmp/myfile.txt
SELECT mycol1,mycol2,mycol3
FROM mytable;
\o
From shell...
echo 'SELECT mycol1,mycol2,mycol3 FROM mytable' | psql myDB >> /tmp/myfile.txt