1

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 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.