Using psycopg2
, I could write large results as CSV using copy_expert
and a BytesIO
buffer like this with pandas
:
copy_sql = "COPY (SELECT * FROM big_table) TO STDOUT CSV"
buffer = BytesIO()
cursor.copy_expert(copy_sql, buffer, size=8192)
buffer.seek(0)
pd.read_csv(buffer, engine="c").to_excel(self.output_file)
However, I can't figure out how to replace the buffer
in copy_expert
with psycopg3
's new copy command. Has anyone figured out a way to do this?
The key to writing a large query to a file through
psycopg3
in this fashion is to use aSpooledTemporaryFile
, which will limit the amount of memory usage in Python (seemax_size
). Then after the CSV is written to disk, convert withpandas
.