How to import CSV file data into a PostgreSQL tabl

2018-12-31 05:42发布

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

13条回答
忆尘夕之涩
2楼-- · 2018-12-31 06:34

You could also use pgAdmin, which offers a GUI to do the import. That's shown in this SO thread. The advantage of using pgAdmin is that it also works for remote databases.

Much like the previous solutions though, you would need to have your table on the database already. Each person has his own solution but what I usually do is open the CSV in Excel, copy the headers, paste special with transposition on a different worksheet, place the corresponding data type on the next column then just copy and paste that to a text editor together with the appropriate SQL table creation query like so:

CREATE TABLE my_table (
    /*paste data from Excel here for example ... */
    col_1 bigint,
    col_2 bigint,
    /* ... */
    col_n bigint 
)
查看更多
登录 后发表回答