PostgreSQL - insertart registro a otra base de dato por dblink

 
Vista:
sin imagen de perfil

insertart registro a otra base de dato por dblink

Publicado por tayshaun (6 intervenciones) el 11/02/2022 15:38:34
Buenos dias Colegas, estoy tratando de crear un cursor en postgresql, para asi insertar esos registro en otro servidor por dblink, la funcion corre bien pero cuando ejecuto mi funcion me dice que la tabla a la que voy a insertar no existe donde quiero insertar (esto lo estoy haciendo desde un servidor cliente que va a insertar la data al principal):

create or replace function public.insertar_reg(
)
returns void
language 'plpgsql'
cost 100
volatile parallel UNSAFE
as $BODY$
declare
v_id prueba_1%rowtype;
v_found_error varchar(30):='no data found';
prueba_cursor cursor for
select * from prueba_1;
begin
open prueba_cursor;
loop
fetch prueba_cursor into v_id;
insert into central
select * from dblink('dbname=center port=5432 host=192.168.56.3 user=postgres password=123', '
select compania,nombre,fecha,estatus from central')
as cust (compania integer, nombre varchar(30),fecha date,estatus varchar(5))
where not exists (select compania from central
where compania=compania
group by compania);
end loop;

update central
set estatus='C'
where fecha between '01-feb-2022' and '02-feb-2022'
and fecha <= now() - interval '7 day';
exception
when no_data_found then
raise exception 'film % not found', v_found_error;
end;
$BODY$;

========================
este es el error que presenta cuando ejecuto la function


ERROR: relation "central" does not exist
LINE 1: insert into central
^
QUERY: insert into central
select * from dblink('dbname=center port=5432 host=192.168.56.3 user=postgres password=123', '
select compania,nombre,fecha,estatus from central')
as cust (compania integer, nombre varchar(30),fecha date,estatus varchar(5))
where not exists (select compania from central
where compania=compania
group by compania)
CONTEXT: PL/pgSQL function insertar_reg() line 11 at SQL statement
SQL state: 42P01
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

insertart registro a otra base de dato por dblink

Publicado por tayshaun (6 intervenciones) el 11/02/2022 18:42:21
aun sigue presentando el mismo error con el dblink_exec



create or replace function public.insertar_reg(
)
returns void
language 'plpgsql'
cost 100
volatile parallel UNSAFE
as $BODY$
declare
v_id prueba_1%rowtype;
v_found_error varchar(30):='no data found';
prueba_cursor cursor for
select * from prueba_1;
begin
open prueba_cursor;
loop
fetch prueba_cursor into v_id;
insert into central
select * from dblink_exec('dbname=center port=5432 host=192.168.56.3 user=postgres password=123', '
select compania,nombre,fecha,estatus from central')
as cust (compania integer, nombre varchar(30),fecha date,estatus varchar(5))
where not exists (select compania from central
where compania=compania
group by compania);
end loop;

update central
set estatus='C'
where fecha between '01-feb-2022' and '02-feb-2022'
and fecha <= now() - interval '7 day';
exception
when no_data_found then
raise exception 'film % not found', v_found_error;
end;
$BODY$;
============

CREATE FUNCTION

Query returned successfully in 121 msec.

=========

select insertar_reg()
muestra el mismo error :

===========

ERROR: relation "central" does not exist
LINE 1: insert into central
^
QUERY: insert into central
select * from dblink_exec('dbname=center port=5432 host=192.168.56.3 user=postgres password=123', '
select compania,nombre,fecha,estatus from central')
as cust (compania integer, nombre varchar(30),fecha date,estatus varchar(5))
where not exists (select compania from central
where compania=compania
group by compania)
CONTEXT: PL/pgSQL function insertar_reg() line 11 at SQL statement
SQL state: 42P01
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Francisco
Val: 256
Oro
Ha mantenido su posición en PostgreSQL (en relación al último mes)
Gráfica de PostgreSQL

insertart registro a otra base de dato por dblink

Publicado por Francisco (110 intervenciones) el 15/02/2022 01:46:12
Hola

Aqui te lo dice

1
2
ERROR: relation "central" does not exist
LINE 1: insert into central

No existe la tabla central

Si lo que quieres es crear la tabla al "vuelo" entonces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
--Se crea la tabla vacia
WITH t AS(
	SELECT
  	*
	FROM
  	DBLINK_EXEC(
	    'dbname=CENTER port=5432 host=192.168.56.3 user=POSTGRES password=123',
	    'SELECT compania, nombre, fecha estatus, FROM central'
  	) AS cust(compania INTEGER, nombre VARCHAR(30), fecha DATE, estatus VARCHAR(5))
)
SELECT * INTO central FROM t WHERE 1<>1;
 
--Llenamos la tabla
WITH t AS(
	SELECT
  	*
	FROM
  	DBLINK_EXEC(
	    'dbname=CENTER port=5432 host=192.168.56.3 user=POSTGRES password=123',
	    'SELECT compania, nombre, fecha estatus, FROM central'
  	) AS cust(compania INTEGER, nombre VARCHAR(30), fecha DATE, estatus VARCHAR(5))
)
INSERT INTO central
SELECT * FROM t
WHERE
  NOT EXISTS (
    SELECT compania FROM central
    WHERE compania = t.compania
    GROUP BY compania
  );

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

insertart registro a otra base de dato por dblink

Publicado por tayshaun (6 intervenciones) el 16/02/2022 01:08:09
buenas gracias por responder, el detalle es que dice eso porque la conexion no se establece cuando dice que (ERROR: relation "central" does not exist), porque la table si existe en el otro servidor que quiero copiar los registro del otro servidor, con este me funciona me inserta los registro en el otro servidor,
----------------------------

create or replace function insert_update_reg(
)
RETURNS void AS
$BODY$
declare
begin
select * from dblink_exec
(
'dbname=master port=5432 host=192.168.56.102 user=postgres password=123',
'insert into master
select compania,nombre,fecha,estatus
from dblink
(
''dbname=slave port=5432 host=192.168.56.101 user=postgres password=123'',
''SELECT compania,nombre,fecha,estatus FROM slave''
)
as cust (compania integer, nombre varchar(30),fecha date,estatus varchar(5))
WHERE not exists(select compania from master
where compania=compania
group by compania );');

end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
---------------------------------------
pero apesar de que copia los registro me presenta esto.(copio los registro del slave para copiarlo al master)

ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function insert_update_reg() line 4 at SQL statement
SQL state: 42601

pero me inserta los registro, como puedo tener un update en esta misma function. se lo agradeceria saber como hacerlo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

insertart registro a otra base de dato por dblink

Publicado por tayshaun (6 intervenciones) el 26/02/2022 14:41:33
gracias por las repuestas ya resolvi con el update

create or replace function insert_update_reg(
)
returns void as
$BODY$
declare
begin
perform dblink(
'dbname=master port=5432 host=192.168.56.102 user=postgres password=123','
insert into master
select compania,nombre,fecha,estatus
from dblink
(
''dbname=slave port=5432 host=192.168.56.101 user=postgres password=123'',
''select compania,nombre,fecha,estatus FROM slave''

)
as cust (compania integer, nombre varchar(30),fecha date,estatus varchar(5))
where not exists(select compania from master
where compania=compania
group by compania)');

perform dblink(
'dbname=master port=5432 host=192.168.56.102 user=postgres password=123','
update master set estatus =''C''
where fecha between ''01-feb-2022'' and ''05-feb-2022'' ');
end;
$BODY$
language 'plpgsql' volatile
cost 100;
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar