将CSV文本数组导入PostgreSQL 9.2

时间:2013-04-16 10:20:39

标签: postgresql csv postgresql-9.2

我的数据是这样的:

  

Akhoond,1,Akhoond,“{”“Akhund”“,”“Akhwan”“}”,0

pgAdmin的导入拒绝了这一点。文本[]需要采用什么格式?

我也试过这个:

  

Akhoond,1,Akhoond,“{Akhund,Akhwan}”,0

这是表创建:

CREATE TABLE private."Titles"
(
  "Abbrev" text NOT NULL,
  "LangID" smallint NOT NULL REFERENCES private."Languages" ("LangID"),
  "Full" text NOT NULL,
  "Alt" text[],
  "Affix" bit
)
WITH (
  OIDS=FALSE
);

ALTER TABLE private."Titles" ADD PRIMARY KEY ("Abbrev", "LangID");

CREATE INDEX ix_titles_alt ON private."Titles" USING GIN ("Alt");

ALTER TABLE private."Titles"
  OWNER TO postgres;

1 个答案:

答案 0 :(得分:9)

找出最佳方法是创建一个包含所需值的表格,并COPY ... TO STDOUT查看:

craig=> CREATE TABLE copyarray(a text, b integer, c text[], d integer);
CREATE TABLE
craig=> insert into copyarray(a,b,c,d) values ('Akhoond',1,ARRAY['Akhund','Akhwan'],0);
INSERT 0 1
craig=> insert into copyarray(a,b,c,d) values ('Akhoond',1,ARRAY['blah with spaces','blah,with,commas''and"quotes'],0);
INSERT 0 1
craig=> \copy copyarray TO stdout WITH (FORMAT CSV)
Akhoond,1,"{Akhund,Akhwan}",0
Akhoond,1,"{""blah with spaces"",""blah,with,commas'and\""quotes""}",0

所以看起来"{Akhund,Akhwan}"很好。请注意我添加的第二个示例,其中显示了如何处理逗号,引用数组文本中的空格。

这适用于psql \copy命令;如果与PgAdmin-III一起工作,那么我建议使用psql\copy