How to import CSV file data into a PostgreSQL table?

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

    Personal experience with PostgreSQL, still waiting for a faster way.

    1. Create table skeleton first if the file is stored locally:

        drop table if exists ur_table;
        CREATE TABLE ur_table
        (
            id serial NOT NULL,
            log_id numeric, 
            proc_code numeric,
            date timestamp,
            qty int,
            name varchar,
            price money
        );
        COPY 
            ur_table(id, log_id, proc_code, date, qty, name, price)
        FROM '\path\xxx.csv' DELIMITER ',' CSV HEADER;
    

    2. When the \path\xxx.csv is on the server, postgreSQL doesn't have the permission to access the server, you will have to import the .csv file through the pgAdmin built in functionality.

    Right click the table name choose import.

    If you still have problem, please refer this tutorial. http://www.postgresqltutorial.com/import-csv-file-into-posgresql-table/

提交回复
热议问题