[英]psql import csv and generate uuid
I'm trying to import from CSV to Postgres and generate a uuid, uuid_generate_v4(), during the import to populate a table.我正在尝试从 CSV 导入 Postgres 并在导入期间生成一个 uuid uuid_generate_v4() 以填充表。
Postgres version: 14 Postgres 版本:14
Postgres Table Postgres 表
Psql import psql 导入
\copy "Country" (select uuid_generate_v4() as "id", "country_name", "country_ISO_code") FROM 'C:\Users\.......\data.csv' (format csv, header, delimiter ',')
However, that throws an error \copy: parse error at "as"
但是,这会引发错误\copy: parse error at "as"
How do I properly instruct psql to use uuid_generate_v4()
as id column?如何正确指示 psql 使用uuid_generate_v4()
作为 id 列?
You can't copy "from" a select statement.您不能“从” select 语句复制。 You need to define a default value for your primary key:您需要为您的主键定义一个默认值:
create table country
(
id uuid primary key default gen_random_uuid(),
country_name text not null,
country_iso_code text not null
);
Then tell the \copy
command that your input file only contains two columns:然后告诉\copy
命令你的输入文件只包含两列:
\copy country (country_name, country_iso_code) from 'data.csv' (format csv, header, delimiter ',')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.