使用Python在两个Postgres数据库直接复制数据
在python脚本中,通过 psycopg2 库的copy_expert,可以很方便地在两个Postgres数据库之间复制数据
1s = StringIO()
2
3# Export into memory buffer
4sql = """
5 COPY (select * from foo) TO STDOUT WITH CSV HEADER ENCODING 'UTF8';
6"""
7
8sourceCursor = sourceConn.cursor()
9sourceCursor.copy_expert(sql, s)
10
11# Import from memory buffer to destination database
12sql = """
13 COPY bar from STDIN WITH CSV HEADER ENCODING 'UTF8';
14"""
15destinationCursor = destinationConn.cursor()
16destinationCursor.copy_expert(sql, s)
参考: