import psycopg2, sys sys.stdout.reconfigure(encoding='utf-8') conn = psycopg2.connect(host='192.168.110.252', port=15432, dbname='postgresql', user='postgresql', password='Jchl1528', options='-c search_path=healthlink_his') cur = conn.cursor() # Find all tables that have del_flag but NOT delete_flag cur.execute(""" SELECT t.table_name FROM information_schema.tables t WHERE t.table_schema = 'healthlink_his' AND EXISTS (SELECT 1 FROM information_schema.columns c WHERE c.table_name = t.table_name AND c.column_name = 'del_flag') AND NOT EXISTS (SELECT 1 FROM information_schema.columns c WHERE c.table_name = t.table_name AND c.column_name = 'delete_flag') """) missing = cur.fetchall() if missing: print('Tables with del_flag but missing delete_flag:') for row in missing: print(' ' + row[0]) cur.execute(f"""ALTER TABLE {row[0]} ADD COLUMN IF NOT EXISTS delete_flag CHAR(1) DEFAULT '0'""") conn.commit() print('All fixed!') else: print('No more tables missing delete_flag') cur.close() conn.close()