为什么在Postgresql控制台中查询正常时查询不会保存在csv文件中 [英] Why query won't save in csv file while it's seems normal in postgresql console
本文介绍了为什么在Postgresql控制台中查询正常时查询不会保存在csv文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此查询,我想保存为csv文件或html
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;
当我在psql控制台中使用它时-似乎很正常.有输出:
When i using it in psql console - it seems normal. There is output:
phone_number | count | driver_callsign
---------------+-------+-----------------
+380502270347 | 2 | 6686
+380502336770 | 2 | 4996
当我使用此命令时:
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
它在那里什么都没写.
如果有人可以帮助修复它,我将不胜感激.
If someone can help to fix it, i will appreciate it.
推荐答案
您非常亲密.
尝试使用 stdout
从控制台通过 psql
将查询的输出定向到文件.下面的示例在客户端计算机上创建一个文件:
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'"
这篇关于为什么在Postgresql控制台中查询正常时查询不会保存在csv文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文