flybonzai flybonzai - 6 months ago 29
Python Question

How to write out to compressed file with pyscopg2.copy_to or copy_expert?

All of the examples I've seen have used

with something like
COPY COMMAND | gzip > 'filename'
. I'd prefer to use a solution with
if possible, and I was thinking that it might be nice to write it out to a string buffer type object which processes the data and then writes out a compressed

How can I do this?


The documentation for psycopg says that copy_to accepts any file-like object. Thus you could simply use the to open a writable gzip file-like object:

import gzip

with'table-data.gz', 'wb') as gzip_file:
    cursor.copy_to(gzip_file, 'my_table')

Alternatively, if you prefer to write text in certain encoding, and on Python 3.3+, you can use mode 'wt' and add encoding='UTF-8' or similar to the