0

I am unable to add pandas dataframe in aws postgres through pandas to_sql

I have tried adding one by one record and it works properly but when i try to add dataframe it doesn't shows any error and it is not committed

import pandas as pd 

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

host="####.amazonaws.com"
port=5432
dbname="@@@@"
user="@@@@"
password="&&&&"

engine=create_engine("@@@@://@@@@:&&&&@####.amazonaws.com:5432/@@@@",
                       echo = True)

pd.read_sql("select * from Cities", engine)

id cityname latitude longitude

0 1 New York City 40.73 -73.93

1 100 abcdef 12.33 14.11

2 100 abcdef -12.33 14.11

tempdf = {'id': 1000, 'cityname': 'Delhi', 'latitude': '120', 'longitude': '110'}

tempdf = pd.DataFrame(list(tempdf.items()))

tempdf.to_sql(name='Cities', con=engine, index=False, if_exists='append')

pd.read_sql("select * from Cities", engine)

id cityname latitude longitude

0 1 New York City 40.73 -73.93

1 100 abcdef 12.33 14.11

2 100 abcdef -12.33 14.11

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Sumit Singh
  • 129
  • 10

1 Answers1

0

Answer linked below appears to suggest adding schema argument to your to_sql statement:

Pandas to_sql doesn't insert any data in my table

MurrayW
  • 393
  • 2
  • 10