postgresql的copy導入數據相當於insert操作,之前必須執行create table xxx(......)
實際業務中一個文件里會包含字段+數據,但是缺少一步到位的導入工具
推薦一個針對csv\xlsx的批量導入工具: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