Don't overwrite data when I upload new using pandas dataframe and postgressql

Question:

I want to start uploading data to my PostgresSql table using pandas. I do the following,

import psycopg2
import pandas as pd
from sqlalchemy import create_engine

user = 'aaa'
passw= 'bbb'
host = 'ccc'
database = 'ddd'
conn_string = f'postgresql://{user}:{passw}@{host}/{database}'

df = pd.read_csv('20220817.csv')

db = create_engine(conn_string)
conn = db.connect()

table = 'tableTest'
df.to_sql(table, con=conn, if_exists='replace', index=False)

conn.close()

The problem is with this code I overwrite the previous data on my table. How can I upload new, without overwriting? I have if_exists=True but my new data is not the same because are time series and change the date and value.

Thank you!

Asked By: Lleims

||

Answers:

You can try by appending as follows:

df.to_sql(table, con=conn, if_exists='append', index=False)

Resource:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_sql.html

Answered By: Harsha Biyani

Replace the argument if_exists='replace' to if_exists='append'

Answered By: Duwang
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.