I wonder of the fastest way to write data from pandas DataFrame to table in postges DB.
1) I\'ve tried pandas.to_sql
, but for some reason it takes entit
Your second approach should be very fast.
There are two problems with your code:
f
you are positioned at the end of the file. You need to put your position back to the beginning before starting to read.Here is what your final code should look like:
import io
f = io.StringIO()
pd.DataFrame({'a':[1,2], 'b':[3,4]}).to_csv(f, index=False, header=False) # removed header
f.seek(0) # move position to beginning of file before reading
cursor = conn.cursor()
cursor.execute('create table bbbb (a int, b int);COMMIT; ')
cursor.copy_from(f, 'bbbb', columns=('a', 'b'), sep=',')
cursor.execute("select * from bbbb;")
a = cursor.fetchall()
print(a)
cursor.close()