I'm having a little difficulty understanding appropriate syntax for the psycopg3
library in Python. I'm trying to copy the contents of a .csv file into my database. The PostgreSQL documentation indicates copy
should be written as follows:
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | PROGRAM 'command' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
[ WHERE condition ]
so I wrote my python statement as follows:
import psycopg
with psycopg.connect('dbname=ideatest user=postgres password=password') as conn:
with conn.cursor() as cur:
mock_idea_info = open(r'C:\dir\filename.csv')
cur.copy('public.ideastorage FROM C:\dir\filename.csv;')
print('Copy successful.')
The problem is that the script prints 'Copy successful,' but does not insert the data into the db. No error messages are generated. I've duplicated the \ characters in the file path, so that isn't the issue. I've been looking around for solutions and possible troubleshooting methods, but have yet to find anything I understand that seems relevant.
Additionally, is there any way I might be able to pass mock_idea_info
directly into the copy
statement?
Any assistance would be immensely appreciated.