I tried to export a select query result to a csv file. I used Postgres \copy metacommand and command line (psql) to do it. But I got a Syntax error and can't understand why. The Query looks fine to me. Maybe the reason for using metacommand instead of COPY? The query
\copy
(
SELECT geo_name, state_us_abbreviation, housing_unit_count_100_percent
FROM us_counties_2010
ORDER BY housing_unit_count_100_percent DESC
LIMIT 20
)
TO '/username/Desktop/us_counties_2010_export.csv'
WITH(FORMAT CSV, HEADER, DELIMITER '|');
Error message
ERROR: syntax error at or near "TO"
LINE 7: TO '/username/Desktop/us_counties_2010_export.csv'
psqlto run this?psqlrunpsql -d yourdb -c "COPY .... TO STDOUT " > /username/Desktop/us_counties_2010_export.csv