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

杀马特。学长 韩版系。学妹 提交于 2021-02-15 06:55:49

问题


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.


回答1:


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'" 


来源:https://stackoverflow.com/questions/59283138/why-query-wont-save-in-csv-file-while-its-seems-normal-in-postgresql-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!