将多个 CSV 文件复制到 postgres [英] Copy multiple CSV files into postgres

查看:77
本文介绍了将多个 CSV 文件复制到 postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 SQL 脚本来将多个 .CSV 文件复制到 postgres 数据库中,如下所示:

I am writing a SQL script to copy multiple .CSV files into a postgres database like this:

COPY product(title, department) from 'ys.csv' CSV HEADER;

我想复制多个文件.我不想:

I have multiple files I want to copy in. I don't want:

COPY product(title, department) from 'ys1.csv' CSV HEADER;
COPY product(title, department) from 'ys2.csv' CSV HEADER;
COPY product(title, department) from 'ys3.csv' CSV HEADER;
COPY product(title, department) from 'ys4.csv' CSV HEADER;
COPY product(title, department) from 'ys5.csv' CSV HEADER;

我想为此使用 for 循环而不是多个复制命令.这可能吗?谢谢

I would like to use a for loop for this instead of multiple copy commands. Is this possible? Thanks

推荐答案

在 linux 中将文件列表的输出通过管道传送到 psql.使 copy 使用标准输入:

In linux pipe the output of the files listing to psql. Make copy use the standard input:

cat /path_to/ys*.csv | psql -c 'COPY product(title, department) from stdin CSV HEADER'

在其他操作系统中寻找等价物

Look for the equivalent in other OSs

这篇关于将多个 CSV 文件复制到 postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆