为什么在Postgresql控制台中查询正常时查询不会保存在csv文件中

时间:2019-12-11 09:59:40

标签: html postgresql csv

我有此查询,我想保存为csv文件或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控制台中使用它时-看起来很正常。有输出:

 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

它在那里什么都没写。

如果有人可以帮助修复它,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

你非常亲密。

尝试使用stdout从控制台通过psql将查询的输出定向到文件。以下示例在客户端计算机上创建一个文件:

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

如果您希望将此输出文件存储在服务器中,则可以尝试以下操作:

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