在 Postgres 9.5.1 中的简单示例数据库中尝试通过 SQL 使用 COPY
命令时……
我收到此错误:
ERROR: invalid input syntax for integer: "Sally"
CONTEXT: COPY customer_, line 2, column id_: "Sally"
********** Error **********
ERROR: invalid input syntax for integer: "Sally"
SQL state: 22P02
Context: COPY customer_, line 2, column id_: "Sally"
...以 CSV(逗号分隔值)格式导入此数据时:
"first_name_","last_name_","phone_","email_"
"Sally","Jones","425.555.1324","[email protected]"
"Jarrod","Barkley","206.555.3454","[email protected]"
"Wendy","Melvin","415.555.2343","[email protected]"
"Lisa","Coleman","425.555.7282","[email protected]"
"Jesse","Johnson","507.555.7865","[email protected]"
"Jean-Luc","Martin","212.555.2244","[email protected]"
...通过在 pgAdmin 中执行的以下 SQL 导入:
COPY customer_
FROM '/home/parallels/Downloads/customer_.csv'
CSV
HEADER
;
...进入这张表:
-- Table: public.customer_
-- DROP TABLE public.customer_;
CREATE TABLE public.customer_
(
id_ integer NOT NULL DEFAULT nextval('customer__id__seq'::regclass),
first_name_ text NOT NULL,
last_name_ text NOT NULL,
phone_ text NOT NULL DEFAULT ''::text,
email_ text NOT NULL DEFAULT ''::text,
CONSTRAINT pkey_customer_ PRIMARY KEY (id_)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.customer_
OWNER TO postgres;
COMMENT ON TABLE public.customer_
IS 'Represents a person whose pets visit our clinic.';
因此,似乎正在成功处理包含列名称的第一行。故障点是 CSV 的第一个数据行中的第一个数据值。我导入的数据都不是整数类型,所以我对错误消息感到困惑。唯一的整数是
id_
主键,自动递增 SERIAL
。我确实阅读了 PG COPY error: invalid input syntax for integer 上的问题页面。但是这个问题确实涉及整数值,并且在被解释为 NULL 的空引用字符串中缺少整数值。就我而言,这里的数据中没有整数值;唯一的整数是带有
SERIAL
生成值的主键 DEFAULT
列(不在导入的数据中)。我还发现了问题 PostgreSQL ERROR: invalid input syntax for integer 。但这似乎无关紧要。
最佳答案
尝试指定列。 。 。没有主键:
COPY customer_ (first_name_ text, last_name_ text, phone_ text, email_ text)
FROM '/home/parallels/Downloads/customer_.csv'
CSV
HEADER
;
没有列列表,它正在寻找id_
的值。导入数据文件的列名的第一行不用于映射到表列。 HEADER标志仅告诉Postgres跳过第一行as documented:
关于postgresql - PG COPY错误: 'invalid input syntax for integer' when importing quoted CSV file without any integers,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36368496/