Cross database query using dblink

Slack Groverglow

I'd like to use dblink in postgres to copy data from a local database to a remote one. This what I came up with:

SELECT * FROM dblink('archive', 'INSERT INTO data SELECT * FROM local.data')

It wont work and I think I understand why. I get the error relation "local.data" does not exist, If possible, Id like to run this inside a stored procedure on the local database, in other words I want to push the data to the 'archive' server, rather than pull it to the 'archive'.

How can I tell postgres to look in the local database? How can I address the local database from inside the dblink query?

Slack Groverglow

I couldn't find a way to do this through dblink, but was able to do it using postgres_fwd. I had to create a foreign table first associated with the table I want to insert into.

CREATE EXTENSION postgres_fdw;
CREATE SERVER archive FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost',  dbname 'archive_db');
CREATE USER MAPPING FOR CURRENT_USER SERVER archive OPTIONS (user 'user', password 'pass');
CREATE FOREIGN TABLE archivedata (..) SERVER archive OPTIONS (schema_name 'public', table_name 'data');

INSERT INTO archivedata SELECT * FROM data;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error on Oracle while using DBLINK

Invalid count and sum in cross tab query using PostgreSQL

PostgreSQL: there is no parameter $1 in dblink query

Is it possible to create a cross-database query with ServiceStack ORMLite?

Using DBeaver to query a PostgreSQL db and get following error: SQL Error [0A000]: ERROR: cross-database references are not implemented

Building queries with a cross database join using SQLalchemy ORM and SQLite

How to do a cross-database-query join in SQLAlchemy?

Function dblink copy and insert into another database

Query the database using discriminator values

unable to query database using timestamp

Using CASE as part of a dblink to get data

need to modify the query without using cross/outer apply

Missing something in connection info String using dblink

Cross-database subquery on using database name stored in record

does dblink use a different session when performed on the same database?

insert using dblink in procedure not working

join on two databases using postgresql and dblink

Postgresql: ERROR: structure of query does not match function result type Using DbLink

Using a function to query a database

query database using variable in eloquent

Can select using dblink, cannot insert using dblink

How can write a query like this using dblink in postgres?

How can you do an update to a table in another database using postgres dblink?

Pro & Cons using dblink postgresql

Methods to avoiding cross-product APOC query (using hashmap?)?

MS Access + Oracle Pass-Through SQL with DBLink to another database

using variables in $_POST to Query Database

Using dblink extension from pg-promise

How to create a table from remote database using dblink