Example with Unix-style file name:
COPY (SELECT * FROM tbl) TO '/var/lib/postgres/myfile1.csv' format csv;
Read the manual about COPY.
You have to use an absolute path for the target file. Be sure to double quote file names with whitespace. Example for MS Windows:
COPY (SELECT * FROM tbl)
TO E'"C:\\Documents and Settings\\Tech\Desktop\\myfile1.csv"' format csv;
In PostgreSQL 8.2, with standard_conforming_strings = off
per default, you need to double backslashes, because \
is a special character and interpreted by PostgreSQL. It's all in the fine manual:
filename
The absolute path name of the input or output file. Windows users might need to use an E'' string and double backslashes used as path
separators.
Or you can also use forward slashes for filenames under Windows.
An alternative is to use the meta-command \copy
of the default terminal client psql
.
Read the manual here.
You can also use a GUI like pgadmin and copy / paste from the result grid to Excel for small queries.
I have written a closely related answer here:
Copy results from a PostgreSQL view in one DB to a table in another
For MySQL, there is a similar solution here:
Exporting MYSQL data into Excel/CSV via php
PostgreSQL
orPostgres
for short. There is noPostgre
. – Erwin Brandstetter Nov 14 '11 at 9:28'C:\\auto_new.txt'
. (Not wrong, though.). There is – Erwin Brandstetter Nov 14 '11 at 12:27TO
. Not sure whether PostgreSQL 8.2 cares. It would still work in 9.0. – Erwin Brandstetter Nov 14 '11 at 12:52