I want to export query result in csv file. I used query select * from entity COPY (select * from entity) TO 'D:/text.csv' WITH DELIMITER AS '|' CSV HEADER; and it workd.. But I want to know if I can do it using spool command like we do it in oracle..
PostgreSQL doesn't have a SPOOL command. You can direct output to a file using COPY (as you've discovered) or by using psql. Psql is a command-line tool. It's not a re-implementation of Oracle's SPOOL.
From the OS prompt
psql -o filename -c 'select * from your_table_name;'
You might have to specify the username and database name, depending on how your login is set up.
From the psql prompt
\o filename
select * from your_table_name;
There are options to control the output. See the -F and -R options, especially.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With