copy data from csv to postgresql using python

前端 未结 8 1179
遥遥无期
遥遥无期 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:45

    I am going to post some of the errors I ran into trying to copy a csv file to a database on a linux based system....

    here is an example csv file:

    Name Age Height
    bob  23   59
    tom  56   67
    
    1. You must install the library psycopg2 (i.e. pip install psycopg2 or sudo apt install python3-psycopg2 )

    2. You must have postgres installed on your system before you can use psycopg2 (sudo apt install postgresql-server postgresql-contrib )

    3. Now you must create a database to store the csv unless you already have postgres setup with a pre-existing database

    COPY CSV USING POSTGRES COMMANDS

    • After installing postgres it creates a default user account which gives you access to postgres commands

    • To switch to the postgres account issue: sudo -u postgres psql

    • Acess the prompt by issuing: psql

      #command to create a database create database mytestdb; #connect to the database to create a table \connect mytestdb; #create a table with same csv column names create table test(name char(50), age char(50), height char(50)); #copy csv file to table copy mytestdb 'path/to/csv' with csv header;

    COPY CSV USING PYTHON The main issue I ran into with copying the CSV file to a database was I didn't have the database created yet, however this can be done with python still.

    import psycopg2 #import the Postgres library
    
    #connect to the database
    conn = psycopg2.connect(host='localhost',
                           dbname='mytestdb',
                           user='postgres',
                           password='')  
    #create a cursor object 
    #cursor object is used to interact with the database
    cur = conn.cursor()
    
    #create table with same headers as csv file
    cur.execute('''create table test(name char(50), age char(50), height char(50));''')
    
    #open the csv file using python standard file I/O
    #copy file into the table just created 
    f = open('file.csv','r')
    cursor.copy_from(f, 'test', sep=',')
    f.close()
    

提交回复
热议问题