Hi. I believe this can be solved more or less like this:
var pg = require('pg');
//bancoA
var conString = "postgres://username:password@localhost/database";
var client1 = new pg.Client(conString);
//bancoB
var conString = "postgres://username:password@localhost/database";
var client2 = new pg.Client(conString);
Now you have connection to both banks.
To return all records from A:
var query = client1.query('select * from table');
//Acho que isso teria que ser feito tabela por tabela
var rows = [];
query.on('row', function(row) {
rows.push(row);
});
//Esse codigo precisa ser testado.
//Nao tenho absoluta certeza de que isso funciona dessa forma.
//Teoricamente a variavel rows estaria com todos os registros
//que o postgres tinha na tabela.
From this point on, we have to define which logic will be used to define which values will be entered in the B-database, since only the records not yet present.
I believe the best way to do this is to make the same select no client2
. From there we would have a value for the variable rows
with the values already inserted in the Bancob table. And then it would be necessary only a simple logic to delete the values of rowsDeA
which already exist in rowsDeB
.
It is worth remembering that, depending on the size of the tables and the periodicity that this will happen, this code can be extremely costly. If the idea is to make it a continuous run (to maintain a backup server, or something like that), maybe it is better to create a logic that inserts the values in Bancob at the same time as in Bancoa.
One more Dlisclaimer: I’m not sure this will work, but it’s worth the test :)