I have a COPY command in psycopg2 which I am running in a loop. I wanted to know how can I append to CSV rather than performing fresh copy everytime.
code:
while True:
cur.execute("COPY (SELECT id,a,b,c,d from t1,t2 WHERE date>= TIMESTAMP TIMESTAMP %(t)s AND date < TIMESTAMP %(t)s + interval '1h' * t2.frequency) \
TO 'path/to/file.csv' DELIMITER ',' CSV;", d)
Thanks in advance.
COPYbeing able to append; however, since you are already using Python, why not just combine all the outputs into a single file after?COPYeach individual file then concatenate them into one file using Python.