Python psycopg2, Copy_from is not working despite sql shell being able to copy the chosen file

Question:

I have a code that backs up selected tables to .csv files using copy_expert.
I tried using these .csv files to restore the tables’ contents with sql shell and it worked but for some reason, when I try using copy_from it just won’t work.

def restore_action():
     connstr2 = clicked2.get() + " dbname=" + db_name.get()
     files_to_restore = []
     for path1 in os.listdir(cur_path+"/backup_files"):
         if os.path.isfile(os.path.join(cur_path+"/backup_files", path1)):
             files_to_restore.append(path1)
     for element in files_to_restore:
         element_csvless = element.removesuffix('.csv')
         conn = psycopg2.connect(connstr2)
         print('Connecting to the PostgreSQL database...')
         cursor = conn.cursor()
         with open(cur_path+"/backup_files/"+element, 'r') as f:
             cursor.copy_from(f, element_csvless, sep=',')
         cursor.close()

Not quite sure where the problem lies

psycopg2.errors.BadCopyFileFormat: extra data after last expected column
CONTEXT: COPY events, line 1: "190552,2020-02-07 14:20:50.651295,1,apa260,"{""Collection"":""apa260"",""dateMili"":634143335643,""E…"

Asked By: Shakalll

||

Answers:

See the comment by @AdrianKlaver

Answered By: Shakalll
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.