This is working perfectly. All the columns in table1 is successfully exported as a .csv file.
(
echo \copy table1 TO '%HOME%\net\CSV_OUTPUT.csv' DELIMITER ',' CSV HEADER;
) | "C:\Program Files\PostgreSQL\9.3\bin\psql.exe" -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
But I wanted to export only selected columns to the .csv file but what I have tried so far does not work.
I have tried using SELECT but it returns an error that says "TO was unexpected at this time"
echo \copy (SELECT red_foo FROM table1) TO '%HOME%\net\CSV_OUTPUT.csv' DELIMITER ',' CSV HEADER;
I have also tried to remove the parentheses but it returns an error that says
"\copy: parse error at "red_foo""
What should be the correct way for this implementation? Your suggestions and ideas are highly appreciated.
Using the suggestion of @a_horse_with_no_name in the comments, I have successfully exported a .csv file with only selected columns.
A separate SQL script that contains the \copy statement is used on the psql command on my windows batch script and it works like a charm.