How to import CSV file data into a PostgreSQL table?

前端 未结 19 2298
再見小時候
再見小時候 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:15

    In Python, you can use this code for automatic PostgreSQL table creation with column names:

    import pandas, csv
    
    from io import StringIO
    from sqlalchemy import create_engine
    
    def psql_insert_copy(table, conn, keys, data_iter):
        dbapi_conn = conn.connection
        with dbapi_conn.cursor() as cur:
            s_buf = StringIO()
            writer = csv.writer(s_buf)
            writer.writerows(data_iter)
            s_buf.seek(0)
            columns = ', '.join('"{}"'.format(k) for k in keys)
            if table.schema:
                table_name = '{}.{}'.format(table.schema, table.name)
            else:
                table_name = table.name
            sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format(table_name, columns)
            cur.copy_expert(sql=sql, file=s_buf)
    
    engine = create_engine('postgresql://user:password@localhost:5432/my_db')
    
    df = pandas.read_csv("my.csv")
    df.to_sql('my_table', engine, schema='my_schema', method=psql_insert_copy)
    

    It's also relatively fast, I can import more than 3.3 million rows in about 4 minutes.

    0 讨论(0)
  • 2020-11-22 03:15

    Create table and have required columns that are used for creating table in csv file.

    1. Open postgres and right click on target table which you want to load & select import and Update the following steps in file options section

    2. Now browse your file in filename

    3. Select csv in format

    4. Encoding as ISO_8859_5

    Now goto Misc. options and check header and click on import.

    0 讨论(0)
  • 2020-11-22 03:16
    COPY table_name FROM 'path/to/data.csv' DELIMITER ',' CSV HEADER;
    
    0 讨论(0)
  • 2020-11-22 03:17

    Take a look at this short article.


    Solution paraphrased here:

    Create your table:

    CREATE TABLE zip_codes 
    (ZIP char(5), LATITUDE double precision, LONGITUDE double precision, 
    CITY varchar, STATE char(2), COUNTY varchar, ZIP_CLASS varchar);
    

    Copy data from your CSV file to the table:

    COPY zip_codes FROM '/path/to/csv/ZIP_CODES.txt' WITH (FORMAT csv);
    
    0 讨论(0)
  • 2020-11-22 03:17

    Use this SQL code

        copy table_name(atribute1,attribute2,attribute3...)
        from 'E:\test.csv' delimiter ',' csv header
    

    the header keyword lets the DBMS know that the csv file have a header with attributes

    for more visit http://www.postgresqltutorial.com/import-csv-file-into-posgresql-table/

    0 讨论(0)
  • 2020-11-22 03:19

    If you need simple mechanism to import from text/parse multiline CSV you could use:

    CREATE TABLE t   -- OR INSERT INTO tab(col_names)
    AS
    SELECT
       t.f[1] AS col1
      ,t.f[2]::int AS col2
      ,t.f[3]::date AS col3
      ,t.f[4] AS col4
    FROM (
      SELECT regexp_split_to_array(l, ',') AS f
      FROM regexp_split_to_table(
    $$a,1,2016-01-01,bbb
    c,2,2018-01-01,ffffd
    e,3,2019-01-01,eee$$, '\n') AS l) t;
    

    DBFiddle Demo

    0 讨论(0)
提交回复
热议问题