psycopg / psycopg-website

Source for the psycopg.org website
https://www.psycopg.org/
4 stars 5 forks source link

articles/2020/11/15/psycopg3-copy/ #20

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

The new COPY support in psycopg3 — Psycopg

Python adapter for PostgreSQL

https://www.psycopg.org/articles/2020/11/15/psycopg3-copy/

ts1g commented 1 year ago

HI Daniele, thank you very much for laying out the updates to psycopg3 (or just psycopg, as you say).

In the example around block-level copy, IDLE complains that a colon is missing after while data := copy.read() but doing so results in an error when using the overall example.

After substituting my_tablename for mytable in the portion that reads COPY mytable TO STDOUTand using the rest of your code, the following error appears for the f.write(data) section:

write() argument must be str, not memoryview

Is the syntax different in newer versions of psycopg? I'm using 3.1.4

dvarrazzo commented 1 year ago

The file should be open in binary mode, using open(FILENAME, "wb"). I will fix the article. Thank you for the report!

ts1g commented 1 year ago

You're welcome; I see the "wb" has been updated. IDLE is still asking for a colon after copy.read() before it will execute, but perhaps that's just my version of Python (3.11)

dss010101 commented 1 year ago

i am getting a 'Cursor' object has no attribute 'copy_expert' error.