将大量数据加载到Postgres Hstore中

use*_*928 10 postgresql hstore

hstore文档仅讨论了一次使用"插入"到一行的hstore.反正有没有批量上传几个100k行,可能是兆字节或Gigs到postgres hstore.

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

有人可以发一个例子吗?优选地,使用python/psycopg的解决方案

nat*_*nat 6

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

$ 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"
Run Code Online (Sandbox Code Playgroud)

同理:

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"
Run Code Online (Sandbox Code Playgroud)

但是,这可以通过导入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)
Run Code Online (Sandbox Code Playgroud)

报价仅允许使用CSV格式,因此需要以CSV格式导入,但您可以使用COPY的DELIMITER和QUOTE参数将字段分隔符和引号字符显式设置为非','和'''值.


dbe*_*hur 3

插入和复制似乎对我来说都很自然

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)
Run Code Online (Sandbox Code Playgroud)