I am using Python 2.6 where I want to read data from Redshift from a table called "user_data" & write to a file using psycopg2. I get the records in "cursor" & in this way I try to write to file :
fout = open('user_data', 'w')
cursor.copy_to(fout,"user_data", sep='|')
When I execute this,it throws following error:
psycopg2.ProgrammingError: syntax error at or near "stdout"
LINE 1: COPY user_data TO stdout WITH DELIMITER AS
What does it wants to tell ?? Whats the solution to this one ??
Unfortunatelly
COPY TO STDOUTis not supported by Amazon Redshift. You can achieve your task in 2 ways. First one is to do as @kadalamittai suggested (iterate cursor and write to file in python) and second is to use theUNLOADcommand. I would recommend the latter when dealing with huge amounts of data.UNLOADenables you to output the result of your query directly to Amazon S3 in CSV file format. Example:More info about the
UNLOADcommand here.You can then use boto to download the file from S3: