Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

html - Why query won't save in csv file while it's seems normal in postgresql console

I have this query which i want to save in csv file or html

select phone_number, count(driver_callsign), driver_callsign from archived_order where data like '%"ptt":3%' and completed is true and ds_id = 16 and created > (select current_date - interval '7 days') group by archived_order.phone_number, archived_order.driver_callsign HAVING COUNT(driver_callsign) > 1;

When i using it in psql console - it seems normal. There is output:

 phone_number  | count | driver_callsign
---------------+-------+-----------------
 +380502270347 |     2 | 6686
 +380502336770 |     2 | 4996

When i'm using this command:

psql -t -A -F ';' -h localhost -U username -c "select phone_number, count(driver_callsign), driver_callsign from archived_order where data like '%"ptt":3%' and completed is true and ds_id = 16 and created > (select current_date - interval '1 days') group by archived_order.phone_number, archived_order.driver_callsign HAVING COUNT(driver_callsign) > 1;" > SomeName.csv

It doesn't writing anything there.

If someone can help to fix it, i will appreciate it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You were very close.

Try using stdout to direct the output of your query to a file using psql from your console. The following example creates a file in the client machine:

$ psql -c "COPY (your query here!) TO STDOUT DELIMITER ';'" > file.csv

If you wish to have this output file in the server you might wanna try this:

$ psql -c "COPY (your query here!) TO '/path/to/file.csv'" 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...