How to import CSV file data into a PostgreSQL table?

前端 未结 19 2661
再見小時候
再見小時候 2020-11-22 02:14

How can I write a stored procedure that imports data from a CSV file and populates the table?

19条回答
  •  醉梦人生
    2020-11-22 03:11

    One quick way of doing this is with the Python pandas library (version 0.15 or above works best). This will handle creating the columns for you - although obviously the choices it makes for data types might not be what you want. If it doesn't quite do what you want you can always use the 'create table' code generated as a template.

    Here's a simple example:

    import pandas as pd
    df = pd.read_csv('mypath.csv')
    df.columns = [c.lower() for c in df.columns] #postgres doesn't like capitals or spaces
    
    from sqlalchemy import create_engine
    engine = create_engine('postgresql://username:password@localhost:5432/dbname')
    
    df.to_sql("my_table_name", engine)
    

    And here's some code that shows you how to set various options:

    # Set it so the raw sql output is logged
    import logging
    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
    
    df.to_sql("my_table_name2", 
              engine, 
              if_exists="append",  #options are ‘fail’, ‘replace’, ‘append’, default ‘fail’
              index=False, #Do not output the index of the dataframe
              dtype={'col1': sqlalchemy.types.NUMERIC,
                     'col2': sqlalchemy.types.String}) #Datatypes should be [sqlalchemy types][1]
    

提交回复
热议问题