How to copy from CSV file to PostgreSQL table with headers in CSV file?

前端 未结 5 1341
陌清茗
陌清茗 2020-12-04 08:51

I want to copy a CSV file to a Postgres table. There are about 100 columns in this table, so I do not want to rewrite them if I don\'t have to.

I am using the

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 09:23

    With the Python library pandas, you can easily create column names and infer data types from a csv file.

    from sqlalchemy import create_engine
    import pandas as pd
    
    engine = create_engine('postgresql://user:pass@localhost/db_name')
    df = pd.read_csv('/path/to/csv_file')
    df.to_sql('pandas_db', engine)
    

    The if_exists parameter can be set to replace or append to an existing table, e.g. df.to_sql('pandas_db', engine, if_exists='replace'). This works for additional input file types as well, docs here and here.

提交回复
热议问题