Discussion:
support for COPY
Joachim Worringen
2010-07-02 12:08:23 UTC
Permalink
Greetings,

I wonder whether psycopg2 support the COPY statement in the sense that
either
- a file which is accessible only by the client gets send to the server
- the server accesses a file local to it

thanks, Joachim
Jan Urbański
2010-07-02 12:21:26 UTC
Permalink
Post by Joachim Worringen
Greetings,
I wonder whether psycopg2 support the COPY statement in the sense that
either
- a file which is accessible only by the client gets send to the server
- the server accesses a file local to it
The former. More specifically, COPY support is psycopg2 means that it
can put the connection in COPY mode and start sending data using the
COPY libpq protocol feature. The psycopg2 implementation wants a
file-like object to provide data for the COPY operation, but that can be
either a disk file or a StringIO object or whatever else that looks like
a Python file.

If you want the server to read files using the SQL COPY command, you can
just do:
cur.execute("COPY tab FROM '/srv/file.dat')

Cheers,
Jan
Manlio Perillo
2010-07-03 20:31:08 UTC
Permalink
Post by Jan Urbański
Post by Joachim Worringen
Greetings,
I wonder whether psycopg2 support the COPY statement in the sense that
either
- a file which is accessible only by the client gets send to the server
- the server accesses a file local to it
The former. More specifically, COPY support is psycopg2 means that it
can put the connection in COPY mode and start sending data using the
COPY libpq protocol feature. The psycopg2 implementation wants a
file-like object to provide data for the COPY operation, but that can be
either a disk file or a StringIO object or whatever else that looks like
a Python file.
By the way, requiring a file object seems to make things more complex as
they should be if COPY data come from a producer; that is data is
produced on the fly.

In this case, what is the best solution using psycopg2 API?



Thanks Manlio
Jan Urbański
2010-07-03 20:59:20 UTC
Permalink
Post by Manlio Perillo
By the way, requiring a file object seems to make things more complex as
they should be if COPY data come from a producer; that is data is
produced on the fly.
In this case, what is the best solution using psycopg2 API?
The producer would just have to implement the read(size) method that
would block until enough data is produced to satisfy the caller and
return a string.

You could write an adapter class that would use a Queue and run the
producer and the COPY operations in separate threads, or things like that.

I'm also planning to add asynchronous COPY support that would make it
possible to use the Twisted producer/consumer interface, if you're doing
Twisted.

Cheers,
Jan

Loading...