Postgresql csv importation that skips rows

孤者浪人 提交于 2019-12-11 04:39:02

问题


I have a Postgresql script that automatically imports csv files into my database. The script can detect duplicate records and remove them, do a proper upsert but still cannot tackle everything. Basically the csv files are exported from other systems which append at the beginning and end of the file extra information e.g:

Total Count: 2956
Avg Time: 13ms

Column1, Column2, Column3
...      ...      ... 

What I want to do is skip those initial rows or any rows at the bottom of the file. Is there any way I can do this in Postgresql via COPY or via another route whatever that might be? Can I call for instance operating system commands via Postgresql?


回答1:


For Linux use tail and head to crop the file and pipe it to your script:

tail -n +3 file.csv | head -1 | psql -f my_script.sql my_database

Then your script will copy from STDIN:

copy my_table from STDIN;


来源:https://stackoverflow.com/questions/16037624/postgresql-csv-importation-that-skips-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!