2016-07-07 23 views
1

工作我试图从一个数据库(PostgreSQL系统)传输大量数据(15二)其他与Python/psycopg2的码头工人。我的码头有4 GB的内存,并且内存不足。Python的psycopg2 - 大数据

我做错了什么?

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() 
+0

到这里看看:http://stackoverflow.com/a/10147451/771848。希望有所帮助。 – alecxe

+0

谢谢。但是我的问题是使用光标,即将所有数据存入内存。 –

回答

0

使用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() 
+0

这解决了这个问题。谢谢。数据流,我必须做什么? –