I need to print a table column out to a text file in postgres, just a simple SELECT "column_name" FROM "table." I'm pretty sure there is a way to do this but I can't remember the syntax. Can anyone help?
1 Answer
Use COPY.
If you need to copy an entire table, you can specify a table name:
COPY country TO '/usr1/proj/bray/sql/country_data';
You can also copy a query result:
COPY (SELECT column_name FROM country WHERE country_name LIKE 'A%')
TO '/usr1/proj/bray/sql/a_list_countries.copy';
The same syntax can be used to import a table:
COPY country FROM '/usr1/proj/bray/sql/country_data';
It is possible to specify additional options, e.g. delimiters, format, etc. In my daily work, I often use:
COPY country TO '/usr1/proj/bray/sql/country_data' DELIMITER ',' CSV;
For a full description of the COPY statement, refer to the linked documentation page above.