Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we export query result in postgres using spooling command?

Tags:

postgresql

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..

like image 586
DMS Avatar asked Mar 23 '26 08:03

DMS


1 Answers

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.

like image 159
Mike Sherrill 'Cat Recall' Avatar answered Mar 26 '26 14:03

Mike Sherrill 'Cat Recall'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!