psql导入csv并生成uuid



我正试图从CSV导入到Postgres,并在导入过程中生成一个uuid,uuid_generate_v4((来填充一个表。

Postgres版本:14

Postgres表

  • 国家
    • id(主键(
    • country_name
    • country_ISO_代码

Psql导入

copy "Country" (select uuid_generate_v4() as "id", "country_name", "country_ISO_code") FROM 'C:Users.......data.csv' (format csv, header, delimiter ',')

然而,这会抛出一个错误copy: parse error at "as"

如何正确地指示psql使用uuid_generate_v4()作为id列?

您不能复制"从";select语句。您需要为主键定义一个默认值:

create table country
(
id uuid primary key default gen_random_uuid(),
country_name text not null,
country_iso_code text not null
);

然后告诉copy命令,您的输入文件只包含两列:

copy country (country_name, country_iso_code) from 'data.csv' (format csv, header, delimiter ',')

最新更新