How to save Amazon Redshift output to local CSV through PSQL on SQL WorkBench?

拜拜、爱过 提交于 2019-12-06 22:38:46

问题


I am writing psql through Amazon Redshift and now I am trying to save the output as CSV through PSQL query, on SQL WorkBench The reason I am planning to do this through query instead of using select clause and then right click to save the output as csv, is because there are large amount of data, I found that if I generate the output into a temp table, it's much much faster than using select to display all the output. Therefore, I am thinking whether saving to local CSV can be faster too.

I have tried the top solution here, however, it doesn't work on Amazon Redshift, When I am using Copy (SELECT col1, col2 FROM my_table) TO '[my local csv path]' WITH CSV DELIMITER ',';, or tried \Copy, it kept showing me

Amazon Invalid operation: syntax error at or near "("

or

Amazon Invalid operation: syntax error at or near "\"

Then I have checked Amazon Redshift query tutorial, didn't find any clause that could save the output to local CSV. It seems that COPY is to copy data from an Amazon data source to Redshift, UNLOAD is to save data to s3, but I just want to save the data on my local machine.

So, is there any way to save the Redshift output to my local CSV through PSQL but on SQL WorkBench?


回答1:


Try running any one of the following in the WorkBench

WbExport -type=text
         -file='C:\Downloads\myData.txt'
         -delimiter='\t'
         -decimal=','
         -dateFormat='yyyy-MM-dd';
select a, b ,c from myTable;

WbExport -type=text
     -file='C:\Downloads\myQuery.txt'
     -delimiter='\t'
     -header=true
     -tableWhere="WHERE a is not null and date between 11/11/11 and 22/22/22"
     -sourcetable=mytable;



回答2:


Yes there is, try this out.

PGPASSWORD=<password> psql -h <host> -d <db> -U <user> -p 5439-a -c "select * from <table>" -F '<delimiter>' -o temp.csv


来源:https://stackoverflow.com/questions/44121467/how-to-save-amazon-redshift-output-to-local-csv-through-psql-on-sql-workbench

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