copy data from csv to postgresql using python

前端 未结 8 1195
遥遥无期
遥遥无期 2021-02-04 03:32

I am on windows 7 64 bit. I have a csv file \'data.csv\'. I want to import data to a postgresql table \'temp_unicommerce_status\' via a python script.

My Script is:

8条回答
  •  故里飘歌
    2021-02-04 03:41

    Here is an extract from relevant PostgreSQL documentation : COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the server. When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server

    That's the reason why the copy command to or from a file a restricted to a PostgreSQL superuser : the file must be present on server and is loaded directly by the server process.

    You should instead use :

    cur.copy_from(r'C:\Users\n\Desktop\data.csv', temp_unicommerce_status)
    

    as suggested by this other answer, because internally it uses COPY from stdin.

提交回复
热议问题