'How to insert new row from a dataframe to PostgreSQL table

I am new in python. I am trying to insert new records from a dataframe to a postgres table. However I observed that everytime existing rows become duplicated.Though I want only new records will be updated into Postgres table and it will ignore existing records. I am using below codes. Can anyone help me on this.

from io import StringIO
from sqlalchemy import create_engine
import psycopg2
import psycopg2.extras as extras

import io
engine = create_engine('postgresql+psycopg2://postgres:Test_1234@localhost:5432/dbname')

selling.head(0).to_sql('html', engine, if_exists='append',index=False)

conn = engine.raw_connection()
cur = conn.cursor()
output = io.StringIO()
selling.to_csv(output, sep='\t', header=False, index=False)
output.seek(0)
contents = output.getvalue()
cur.copy_from(output, 'html', null="")
conn.commit()



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source