How can I write a stored procedure that imports data from a CSV file and populates the table?
相关问题
- sqlyog export query result as csv
- Django distinct is not working
- PostgreSQL: left outer join syntax
- Connecting Python to a Heroku PostgreSQL DB?
- PostgreSQL - Deleting data that are older than an
相关文章
- postgresql 关于使用between and 中是字符串的问题
- postgresql 月份差计算问题
- Using boolean expression in order by clause
- Table valued Parameter Equivalent in Postgresql
- in redshift postgresql can I skip columns with the
- How to read local csv file in client side javascri
- Oracle equivalent of PostgreSQL INSERT…RETURNING *
- PostgreSQL field data type for IPv4 addresses
create a table first
Then use copy command to copy the table details:
copy table_name (C1,C2,C3....)
from 'path to your csv file' delimiter ',' csv header;
Thanks
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/
If you need simple mechanism to import from text/parse multiline CSV you could use:
DBFiddle Demo
Personal experience with PostgreSQL, still waiting for a faster way.
1. Create table skeleton first if the file is stored locally:
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/
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 and column count of your csv file, you can use the following function to load your table to a temp table that will be named as
target_table
:The top row is assumed to have the column names.
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.