export a sql query to csv in psql code example Example: postgresql export output table as a csv COPY table_name TO 'file_name.csv' DELIMITER ',' CSV HEADER;