我一直在努力应对其他StackOverflow响应。我想将查询的输出保存到本地文本文件 - 只要它在我的本地计算机上,文本文件的位置并不重要。
我正在使用的代码:
\COPY (
select month,count(*) as distinct_Count_month
from
(
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month
FROM yi_fourmpanel.card_panel WHERE COBRAND_ID = '10006164'
group by UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM')
) a
group by month) TO 'mycsv.csv' WITH CSV HEADER;
此代码出错:
<!-- language: none -->
An error occurred when executing the SQL command:
\COPY (
ERROR: syntax error at or near "\"
Position: 1
\COPY (
^
Execution time: 0.08s
(Statement 1 of 2 finished)
An error occurred when executing the SQL command:
select month,count(*) as distinct_Count_month
from
(
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month
FROM yi_fourmpanel.card_panel...
ERROR: syntax error at or near ")"
Position: 260
group by month) TO 'mycsv.csv' WITH CSV HEADER
^
Execution time: 0.08s
(Statement 2 of 2 finished)
2 statements failed.
Script execution finished
Total script execution time: 0.16s
答案 0 :(得分:0)
1.对于服务器COPY ,请移除 18, 94, 189, 105, 32, 153, 68, 159, 178, 34
并在以下\
中运行:
psql
COPY (
WITH data(val1, val2) AS ( VALUES
('v1', 'v2')
)
SELECT *
FROM data
) TO 'yourServerPath/output.csv' CSV HEADER;
:
cat yourServerPath/output.csv
2.对于客户端COPY :
val1,val2
v1,v2
psql -h host -U user -d database -c "\copy \
( \
WITH data(val1, val2) AS ( VALUES \
(1, 2) \
) \
SELECT * FROM data) TO 'yourClientPath/output.csv' CSV HEADER;"
:
cat yourClientPath/output.csv
例如,在终端中的客户端计算机上,您需要使用绝对路径运行以下脚本val1,val2
1,2
:
mycsv.csv