postgres sql insert into

0

I have to copy data from two databases to a new one, it happens that when you copied new data these already exist but there are new ones that are not inserted.

The query I have at the moment is this:

INSERT INTO concelho(id,censo,concelho)
SELECT "CONCELHO","CENSO","CONCELHO_DSG"
FROM dblink('dbname=meu_db host=meu_host user=meu_user password=minha_senha' ::text,'SELECT "CONCELHO","CENSO","CONCELHO_DSG" FROM "MUNICIPIO (Concelho)"' ::text,false) 
as tabela_temp("CONCELHO" varchar, "CENSO" varchar, "CONCELHO_DSG" character varying (200))
    
asked by anonymous 01.01.2016 / 17:13

2 answers

1

Already resolved

'INSERT INTO concelho(id,censo,concelho)´SELECT "CONCELHO","CENSO","CONCELHO_DSG"FROM dblink('dbname=meu_db host=meu_hostuser=meu_user password=minha_senha'::text,'SELECT"CONCELHO","CENSO","CONCELHO_DSG" FROM"MUNICIPIO (Concelho)"' ::text,false) as tabela_temp("CONCELHO" varchar,"CENSO" varchar, "CONCELHO_DSG" character varying (200))WHERE    NOT EXISTS (SELECT id,censo,concelho FROM concelho WHERE id = tabela_temp."CONCELHO" 
);'
    
02.01.2016 / 14:53
0
insert into concelho(id,censo,concelho)
select "CONCELHO","CENSO","CONCELHO_DSG"
from dblink(
    'dbname=meu_db host=meu_host user=meu_user password=minha_senha' ::text,
    'SELECT "CONCELHO","CENSO","CONCELHO_DSG" FROM "MUNICIPIO (Concelho)"' ::text,false)
as tabela_temp("CONCELHO" varchar, "CENSO" varchar, "CONCELHO_DSG" character varying (200))
except
select "CONCELHO","CENSO","CONCELHO_DSG"
from concelho
    
02.01.2016 / 14:12