How can I write a stored procedure that imports data from a CSV file and populates the table?
|
Take a look at this short article. Solution paraphrased here: Create your table:
Copy data from your CSV file to the table:
|
|||||||||||||||||||||
|
If you don't have permission to use Create your table:
Copy data from your CSV file to the table:
You can also specify the columns to read:
|
|||||||||||||||||
|
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:
And here's some code that shows you how to set various options:
|
|||||||||||||
|
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:
|
||||
|
As Paul mentioned, import works in pgAdmin: right click on table -> import select local file, format and coding here is a german pgAdmin GUI screenshot: similar thing you can do with DbVisualizer (I have a license, not sure about free version) right click on a table -> Import Table Data... |
|||||
|
Most other solutions here require that you create the table in advance/manually. This may not be practical in some cases (e.g., if you have a lot of columns in the destination table). So, the approach below may come handy. Providing the "path to" and "the number of columns" in your csv file, you can use the following function to load your table to a
Columns are named as: col_1, col_2, ..., etc. Furthermore, in most typical scenarios, the top row usually have the column names. To rename the columns as such, you can use the following, a bit more sophisticated version:
|
|||||||||
|
|
||||
|
Use this SQL code
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/ |
||||
|
IMHO, the most convenient way is to follow "Import CSV data into postgresql, the comfortable way ;-)", using csvsql from csvkit, which is a python package installable via pip. |
|||||||||||||||||||||
|
Maybe this tool be helpful http://www.convertcsv.com/csv-to-sql.htm It have a lot of configuration options to convert from CSV to database script. |
|||
|