【问题标题】:Python psycopg2 - Work with large dataPython psycopg2 - 处理大数据
【发布时间】:2016-11-10 06:47:51
【问题描述】:

我正在尝试使用 docker 上的 python/psycopg2 将大数据 (15 B) 从一个数据库 (postgresql) 传输到另一个数据库。我的 docker 有 4 GB 的内存并且内存不足。

我做错了什么?

cursor = conn.cursor()
cursor.execute('select * from schema.table')
for row in cursor:
   tp = tuple(map(lambda x: x.encode('utf-8'), row)
   cursor.execute('Insert into table2 values {}'.format(tp))
   conn.commit()

【问题讨论】:

标签: python postgresql docker psycopg2


【解决方案1】:

使用copy_to and copy_from

f = open('t.txt', 'wb')
conn = psycopg2.connect(database='source_db') 
cursor = conn.cursor() 
cursor.copy_to(f, 'source_table')
conn.close()
f.close()

f = open('t.txt', 'r')
conn = psycopg2.connect(database='target_db') 
cursor = conn.cursor() 
cursor.copy_from(f, 'target_table')
conn.close()
f.close()

【讨论】:

  • 这样就解决了问题。谢谢。有了数据流,我该怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 2020-07-15
  • 2011-07-28
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多