I have read and tried a lot of posts on stackoverflow about adding a pandas dataframe to an existing postgresql database.
Here is my code
conn=psycopg2.connect(dbname=’mypostgres’, user=’…’, password=”…”, host=’127.0.0.1′,
sslmode=’require’)
cur=conn.cursor()
cur.execute(“””
INSERT INTO mypostgres
(region,state,tier,v_detailed,
v_approx,
v_unmapped,
v_total,
a_detailed,
a_approx,
a_unmapped,
a_total) VALUES (‘moscow’,’moscow’,’moscow’,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15)
“””)
conn.commit()
data={‘Name’: [‘Tom’, ‘dick’, ‘harry’],
‘Age’: [22, 21, 24]}
dff=pd.DataFrame(data)
signin_info=’postgresql://’+sign_in[‘user’]+’:’+sign_in[‘password’]+’@’+sign_in[‘host’]+’:’+sign_in[‘port’]+’/’+sign_in[‘database’]
engine=create_engine(signin_info)
dff.to_sql(‘data’,con=engine, if_exists=’replace’)
However, I see that the dataframe data is not added.
I need to add a pandas dataframe to an existing mypostgres database.
What should I do?
0