Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy data from csv to postgresql using python

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:

import psycopg2
conn = psycopg2.connect("host='localhost' port='5432' dbname='Ekodev' user='bn_openerp' password='fa05844d'")
cur = conn.cursor()
cur.execute("""truncate table "meta".temp_unicommerce_status;""")
cur.execute("""Copy temp_unicommerce_status from 'C:\Users\n\Desktop\data.csv';""")
conn.commit()
conn.close()

I am getting this error

Traceback (most recent call last):
  File "C:\Users\n\Documents\NetBeansProjects\Unicommerce_Status_Update\src\unicommerce_status_update.py", line 5, in <module>
cur.execute("""Copy temp_unicommerce_status from     'C:\\Users\\n\\Desktop\\data.csv';""")
psycopg2.ProgrammingError: must be superuser to COPY to or from a file
HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.
like image 613
Manish Gupta Avatar asked May 05 '15 10:05

Manish Gupta


People also ask

How do I import a CSV file into PostgreSQL using Python?

First, we import the psycopg2 package and establish a connection to a PostgreSQL database using the pyscopg2. connect() method. before importing a CSV file we need to create a table. In the example below, we created a table by executing the “create table” SQL command using the cursor.

How do I import CSV data into PostgreSQL table using PgAdmin?

To import CSV using this PgAdmin Import CSV method, you have to do the following: Click on the Tools tab at the top of your PgAdmin Home Page. Select the Query Tool in the drop-down menu that appears. Enter the title and columns in your CSV file as an SQL Query.


2 Answers

Use the copy_from cursor method

f = open(r'C:\Users\n\Desktop\data.csv', 'r') cur.copy_from(f, temp_unicommerce_status, sep=',') f.close() 

The file must be passed as an object.

Since you are coping from a csv file it is necessary to specify the separator as the default is a tab character

like image 176
Clodoaldo Neto Avatar answered Sep 21 '22 21:09

Clodoaldo Neto


The way I solved this problem particular to use psychopg2 cursor class function copy_expert (Docs: http://initd.org/psycopg/docs/cursor.html). copy_expert allows you to use STDIN therefore bypassing the need to issue a superuser privilege for the postgres user. Your access to the file then depends on the client (linux/windows/mac) user's access to the file

From Postgres COPY Docs (https://www.postgresql.org/docs/current/static/sql-copy.html):

Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.

You can also leave the permissions set strictly for access to the development_user home folder and the App folder.

csv_file_name = '/home/user/some_file.csv'
sql = "COPY table_name FROM STDIN DELIMITER '|' CSV HEADER"
cursor.copy_expert(sql, open(csv_file_name, "r"))
like image 28
raw-bin hood Avatar answered Sep 21 '22 21:09

raw-bin hood