postgresql: cómo obtener datos entre diferentes base de datos
Fuente: http://stackoverflow.com/questions/6083132/postgresql-insert-into-select
Fuente: http://roy-rc.blogspot.com/2010/08/postgresql-dblink-conexion-entre-bases.html
A veces es necesario poder copiar datos entre diferentes bases de datos, que incluso pueden residir en diferentes servidores. Postgresql proporciona una librería interesante para poder realizar dicho cometido.
|
1
|
# aptitude install postgresql-contrib-8.3 |
|
1
2
|
$ cd /usr/share/postgresql/8.3/contrib $ psql test_db u_test -h localhost < dblink.sql |
Te conectas a la BD que tiene instalado el DBlink y realizas la consulta
|
1
|
$ psql test_db u_test -h localhost |
|
1
2
3
|
test_db=# select * from dblink ('dbname=my_db hostaddr=191.168.50.90 user=u_test password=123456 port=5432', 'select id,descripcion from tabla') as t1(id int4,descripcion text); |
Por ejemplo:
En la bbdd1 creamos una tabla:
psql dbtest CREATE TABLE tblB (id serial, time integer);
INSERT INTO tblB (time) VALUES (5000), (2000);
En la bbd2, creamo otra tabla, y la alimentaremos desde la tabla que reside en otra instancia:
psql postgres CREATE TABLE tblA (id serial, time integer);
INSERT INTO tblA SELECT id, time FROM dblink(‘dbname=dbtest’, ‘SELECT id, time FROM tblB’) AS t(id integer, time integer) WHERE time > 1000;
illatà !