How to save PostgreSQL psql query output in CSV or TEXT format to a file

preview_player
Показать описание
sql - Save PL/pgSQL output from PostgreSQL to a CSV file
postgresql - psql - save results of command to a file
Save PL/pgSQL output from PostgreSQL to a CSV file
shell - How to export a PostgreSQL query output to a csv file
Saving an output of PostgreSQL query to a text file
PostgreSQL how to redirect output to a file
Simple Export to CSV with Postgres Example
OR
OR
Рекомендации по теме
Комментарии
Автор

I have a complex query like this:
SELECT id, c1, c2, c3, c4
FROM (
SELECT
*
, RANK() OVER (PARTITION BY id ORDER BY c4 DESC) c4rank
FROM t
) ranked
WHERE c4rank = 1
AND id = 'something'


I'm unable to execute
\copy (query) TO '/someplace/abc.csv' (format CSV);

It is giving me syntax error.

kakibollywoodremixes