如果这是一项临时任务
使用输入文件中的所有列创建一个临时表
create temporary table t (x1 integer, ... , x10 text)
从文件中复制到其中:
copy t (x1, ... , x10)
from '/path/to/my_file'
with (format csv)
现在从临时表插入最终表:
insert into my_table (x2, x5, x7, x10)
select x2, x5, x7, x10
from t
并删除它:
drop table t
如果这是一个频繁的任务
Use the file_fdw扩大。作为超级用户:
create extension file_fdw;
create server my_csv foreign data wrapper file_fdw;
create foreign table my_csv (
x1 integer,
x2 text,
x3 text
) server my_csv
options (filename '/tmp/my_csv.csv', format 'csv' )
;
向将读取该表的用户授予对该表的选择权限:
grant select on table my_csv to the_read_user;
然后,只要有必要,就可以像读取表格一样直接从 csv 文件中读取:
insert into my_table (x2)
select x2
from my_csv
where x1 = 2