长风秋雁
这就是我做的。可能更快,因为它正在使用execute_batch:# df is the dataframeif len(df) > 0: df_columns = list(df) # create (col1,col2,...) columns = ",".join(df_columns) # create VALUES('%s', '%s",...) one '%s' per column values = "VALUES({})".format(",".join(["%s" for _ in df_columns])) #create INSERT INTO table (columns) VALUES('%s',...) insert_stmt = "INSERT INTO {} ({}) {}".format(table,columns,values) cur = conn.cursor() psycopg2.extras.execute_batch(cur, insert_stmt, df.values) conn.commit() cur.close()