0

I have a pandas DataFrame,df, that I want to append to a remote table TabB on a postgres db. Both tables shares the same columns. How can I perform this using pandas and sql alchemy?

My sqlalchemy engine is defined as this:

myengine=create_engine('postgresql://postgres:postgres@xxx.xx.xxx.xxx:5433/mydb') 

I would like to know if I can apply a method like df.read_sql() to pass a proper INSERT TO. Any example would be greatly appreciated. Thanks in advance!

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Giorgio Spedicato
  • 2,413
  • 3
  • 31
  • 45

1 Answers1

1

In pandas=0.20.3 I generally use something like that:

db_engine=create_engine('postgresql://postgres:postgres@xxx.xx.xxx.xxx:5433/mydb')
df_to_save.to_sql(con=db_engine, name='yourTable', if_exists='append', index=False)

df_to_save and yourTable needs to have the same columns

nicor88
  • 1,398
  • 10
  • 13