I am trying to import the following table to my Postgres server using cursor.copy_from() in psycopg2 because the file is too large.
| id | name | |
|---|---|---|
| 1 | john123@gmail.com | John Stokes |
| 2 | emily123@gmail.com | Emily Ray |
Here is my code:
import psycopg2
import os
conn = psycopg2.connect(
dbname = name,
user = username,
password = pwd,
host = hst,
port = 5432
)
cur = conn.cursor()
path = os.getcwd() + '\users.csv'
file = open(path, 'r')
cur.copy_from(file, table_name, sep=',')
conn.commit()
conn.close()
This inserts the data to the table but there is double quotes in the third column like below.
| id | name | |
|---|---|---|
| 1 | john123@gmail.com | "John Stokes" |
| 2 | emily123@gmail.com | "Emily Ray" |
Later I found out that the problem lies in the open() itself. Because if I print the first line by doing file.readline(), I get:
1,john123@gmail.com,"John Stokes"
I don't want these double quotes in my table. I tried using cursor.execute() with COPY FROM query but it says that I am not a superuser even if I am.