postgresql的copy导入数据相当于insert操作,之前必须执行create table xxx(......)
实际业务中一个文件里会包含字段+数据,但是缺少一步到位的导入工具
推荐一个针对csvxlsx的批量导入工具:https://github.com/zhengze/file_to_db.git
如果文件第一行是table的字段名,自动建表+导入数据,实在是省事。
具体用法请看README:
load data from csv/xlsx into database(mysql/postgresql)
INSTALL
pipenv install #或者
pip install -r requirements.txt
CONFIG
- $sudo touch .env
DATABASE_URI=postgresql+psycopg2://admin:1234@localhost/test
USAGE
python file_to_db.py -h
usage: file_to_db.py [-h] [--file FILE] [--table TABLE]
optional arguments:
-h, --help show this help message and exit
--file FILE, -f FILE the file path.
--table TABLE, -t TABLE
the table name.
示例:
python file_to_db.py -f user -t ./test.csv
test.csv内容如下:
name,age zhangsan,15