'copying a table across servers with psycopg3

I'm working from and example in the psycopg3 documentation to copy a table from one database to another: link

dsn_src = 'postgresql:///dev_db'
dsn_tgt = 'postgresql:///prod_test'

with psycopg.connect(dsn_src) as conn1, psycopg.connect(dsn_tgt) as conn2:
    with conn1.cursor().copy("COPY sample TO STDOUT (FORMAT BINARY)") as copy1:
        with conn2.cursor().copy("COPY sample FROM STDIN (FORMAT BINARY)") as copy2:
            for data in copy1:
                copy2.write(data)

running this results in the following error

QueryCanceled: COPY from stdin failed: error from Python: TypeError - can't write memoryview
CONTEXT:  COPY sample, line 1

The source and target schema are identical, as the documentation recommends, and this error persists if the format specification (FORMAT BINARY) is removed.

Is there are way to resolve this memoryview error?



Solution 1:[1]

Guessing you are using psycopg3 <= 3.0.11. This was fixed in 3.0.12 per Release Notes. I ran the code in 3.0.11 and it failed as you showed. I upgraded to 3.0.13 and it worked.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian Klaver