我有大约 6000 个 IPv6 地址需要加载到 postgres 表中。甚至没有找到我的“如果不存在错误”的地步,它在未转义的“:”上很糟糕,我不太了解 postgres,是否有一个 LOAD DATA INFILE 函数会在行中读取并忽略“: ' 以及查找现有记录?
INSERT INTO ip_list (ip_addr)
SELECT 'ip_addr',
2600:3c01:e000:44:0:0:0:1,
2600:3c01:e000:44:0:0:0:2,
2600:3c01:e000:44:0:0:0:3,
2600:3c01:e000:44:0:0:0:4,
2600:3c01:e000:44:0:0:0:5,
2600:3c01:e000:44:0:0:0:7,
....
WHERE NOT EXIST(
SELECT 1 FROM ip_list WHERE name = 'ip_addr')
RETURNING id
);
ERROR: syntax error at or near ":"
LINE 3: 2600:3c01:e000:44:0:0:0:1,
更新:
此方法从不上传任何记录:
You are now connected to database "postfix" as user "postgres".
postfix=# create temporary table t(ip_addr inet);
CREATE TABLE
postfix=# \copy t from '/var/www/localhost/htdocs/ipListScript'
postfix=# INSERT INTO ip_list(ip_addr)
select ip_addr from ip_list where
not exists (select 1 from ip_list where ip_list.ip_addr=ip_list.ip_addr);