我正在编写一个脚本,使用psycopg2在同一网络上的两台机器之间复制一些数据.我正在用一些旧的,丑陋的bash替换副本
psql -c -h remote.host "COPY table TO STDOUT" | psql -c "COPY table FROM STDIN"
Run Code Online (Sandbox Code Playgroud)
这似乎是最简单,最有效的复制方式.使用stringIO或临时文件在python中复制很容易,如下所示:
buf = StringIO()
from_curs = from_conn.cursor()
to_curs = to_conn.cursor()
from_curs.copy_expert("COPY table TO STDOUT", buf)
buf.seek(0, os.SEEK_SET)
to_curs.copy_expert("COPY table FROM STDIN", buf)
Run Code Online (Sandbox Code Playgroud)
...但这涉及将所有数据保存到磁盘/内存中.
有没有人想出一种方法来模仿像这样的副本中的Unix管道的行为?我似乎无法找到一个不涉及POpen的unix-pipe对象 - 也许最好的解决方案就是使用POpen和subprocess,毕竟.