使用Psycopg3复制跨服务器的表格

发布于 2025-01-29 05:43:48 字数 843 浏览 3 评论 0 原文

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

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)

在以下错误中运行此错误

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

,源和目标架构是相同的,如文档所建议,如果格式规范(格式二进制)被删除。

是否有解决此 MemoryView 错误的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2025-02-05 05:43:48

猜测您正在使用 psycopg3 < = 3.0.11。将其固定在3.0.12 per 发行说明。我以3.0.11运行代码,如您所示,它失败了。我升级到3.0.13,它起作用了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文