To reset tables in postgresql with psycopg2 you can use
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
connection = psycopg2.connect(user=User,
password=Paswd,
host=Host,
port=Port,
database=Db)
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = connection.cursor()
def reset_tables(tables):
for table in tables:
delete_query = f"DELETE FROM {table};"
cursor.execute(delete_query)
cursor.execute(f'TRUNCATE {table} RESTART IDENTITY;')
cursor.execute(f'ALTER SEQUENCE {table}_id_seq RESTART WITH 1;')
connection.commit()
tables = ['table1', 'table2']
reset_tables(tables)