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

 



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-12-02
  • 2022-12-23
  • 2021-11-20
  • 2021-06-10
  • 2021-12-26
猜你喜欢
  • 2021-10-07
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2021-09-25
相关资源
相似解决方案