L
L
lnk_link2021-10-08 13:06:34
PostgreSQL
lnk_link, 2021-10-08 13:06:34

Psycopg2: Importing csv to postgres, how to do it?

Please tell me, I'm trying to import a csv file into a pg table. I am using psycopg2 and jupyter-notebook.

import psycopg2
conn = psycopg2.connect("host='localhost' port='5432' dbname='postgres' user='postgres' password='*******'")
cur = conn.cursor()
cur.execute("""truncate table "sandbox".promolist;""")
f = open(r'test.csv', 'r')
cur.copy_from(f, "sandbox.promolist", sep=';', null = "")
f.close()
conn.commit()
conn.close()


However, I am getting an error:

UndefinedTable                            
<ipython-input-2-149d5141a140> in <module>
      4 cur.execute("""truncate table "sandbox".promolist;""")
      5 f = open(r'test.csv', 'r')
----> 6 cur.copy_from(f, "sandbox.promolist", sep=';', null = "")
      7 f.close()
      8 conn.commit()

UndefinedTable: ОШИБКА:  отношение "sandbox.promolist" не существует


The diagram is correct, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-10-08
@galaxy

Changed in version 2.9: the table and fields names are now quoted. If you need to specify a schema-qualified table please use copy_expert() .

https://www.psycopg.org/docs/cursor.html#cursor.co...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question