我正在嘗試從 CSV 匯入到 Postgres 并在匯入期間生成一個 uuid、uuid_generate_v4() 以填充表。
Postgres 版本:14
Postgres 表
- 國家
- id(主鍵)
- 國家的名字
- country_ISO_Code
sql匯入
\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"
如何正確指示 psqluuid_generate_v4()用作 id 列?
提前致謝
uj5u.com熱心網友回復:
您不能“從”選擇陳述句復制。您需要為主鍵定義一個默認值:
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 ',')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372784.html
標籤:PostgreSQL的 文件 psql
上一篇:使用Bash腳本編輯CSV檔案
