使用python向postgres插入数据
下面是我用来将数据从一台postgres服务器推送到另一台postgres服务器的代码示例。我正试图移动2800万张唱片。这在SQLServertoPostgres上运行得很好,但现在是PostgretoPostgres,它挂线了 sourcecursor.execute('select*from“schema”。'reallyllargetable') 它从未到达任何其他语句以到达迭代器 我得到这个信息: psycopg2.DatabaseError:查询结果和select查询语句的内存不足使用python向postgres插入数据,python,postgresql,Python,Postgresql,下面是我用来将数据从一台postgres服务器推送到另一台postgres服务器的代码示例。我正试图移动2800万张唱片。这在SQLServertoPostgres上运行得很好,但现在是PostgretoPostgres,它挂线了 sourcecursor.execute('select*from“schema”。'reallyllargetable') 它从未到达任何其他语句以到达迭代器 我得到这个信息: psycopg2.DatabaseError:查询结果和select查询语句的内存不足
#cursors for aiods and ili#
sourcecursor = sourceconn.cursor()
destcursor= destconn.cursor()
#name of temp csv file
filenme= 'filename.csv'
#defenition that uses fetchmany to iterate through data in batch. default
value is in 10000#
def ResultIterator(cursor, arraysize=1000):
'iterator using fetchmany and consumes less memory'
while True:
results = cursor.fetchmany(arraysize)
if not results:
break
for result in results:
yield result
#set data for the cursor#
print("start get data")
#it is not going past the line below. it errors at with out of memory for query result
sourcecursor.execute('select * from "schema"."reallylargetable"; ')
print("iterator")
dataresults= ResultIterator(sourcecursor)
*****do something with dataresults *********
请更改此行:
sourcecursor = sourceconn.cursor()
要命名光标(使用任何您喜欢的名称):
这样做的目的是指导psycopg2为您的查询打开一个名为cursor的postgresql服务器端。在服务器端没有命名游标的情况下,psycopg2会在执行查询时尝试获取所有行
sourcecursor = sourceconn.cursor('mysourcecursor')