Postgresql 将大量数据加载到Postgres Hstore

Postgresql 将大量数据加载到Postgres Hstore,postgresql,hstore,Postgresql,Hstore,hstore文档只讨论在hstore中一次使用一行“insert”。 有没有办法批量上传几行10万行 它可以是进入postgres hstore的兆字节或千兆字节 复制命令似乎仅适用于上载csv文件列 有人能举个例子吗?最好是使用python/psycopg的解决方案,插入和复制对我来说都是自然的 create table b(h hstore); insert into b(h) VALUES ('a=>1,b=>2'::hstore), ('c=>2,d=>3'::h

hstore文档只讨论在hstore中一次使用一行“insert”。 有没有办法批量上传几行10万行 它可以是进入postgres hstore的兆字节或千兆字节

复制命令似乎仅适用于上载csv文件列


有人能举个例子吗?最好是使用python/psycopg的解决方案,插入和复制对我来说都是自然的

create table b(h hstore);
insert into b(h) VALUES ('a=>1,b=>2'::hstore), ('c=>2,d=>3'::hstore);
select * from b;
         h          
--------------------
 "a"=>"1", "b"=>"2"
 "c"=>"2", "d"=>"3"
(2 rows)

$ cat > /tmp/t.tsv
a=>1,b=>2
c=>2,d=>3
^d

copy b(h) from '/tmp/t.tsv';
select * from b;
         h          
--------------------
 "a"=>"1", "b"=>"2"
 "c"=>"2", "d"=>"3"
 "a"=>"1", "b"=>"2"
 "c"=>"2", "d"=>"3"
(4 rows)

上面的答案似乎不完整,因为如果您尝试在多个列中复制,包括一个具有hstore类型的列,并使用逗号分隔符,则复制会混淆,如:

$ cat test
1,a=>1,b=>2,a
2,c=>3,d=>4,b
3,e=>5,f=>6,c

create table b(a int4, h hstore, c varchar(10));
CREATE TABLE;
copy b(a,h,c) from 'test' CSV;
ERROR:  extra data after last expected column
CONTEXT:  COPY b, line 1: "1,a=>1,b=>2,a"
同样地:

copy b(a,h,c) from 'test' DELIMITER ',';
ERROR:  extra data after last expected column
CONTEXT:  COPY b, line 1: "1,a=>1,b=>2,a"
但是,可以通过导入为CSV并引用要导入到hstore中的字段来解决此问题:

$ cat test
1,"a=>1,b=>2",a
2,"c=>3,d=>4",b
3,"e=>5,f=>6",c

copy b(a,h,c) from 'test' CSV;
COPY 3
select h from b;
         h          
--------------------
 "a"=>"1", "b"=>"2"
 "c"=>"3", "d"=>"4"
 "e"=>"5", "f"=>"6"
(3 rows)

只允许在CSV格式中使用引号,因此需要作为CSV导入,但您可以使用用于复制的分隔符和引号参数将字段分隔符和引号字符显式设置为非“,”和“.”值。

您完全可以使用COPY binary命令执行此操作

我不知道python库可以做到这一点,但我有一个ruby库可以帮助您理解列编码