Postgresql foreign data wrapper - postgresql

hoping for some help as I am very new to postgresql admin!
I have 2 servers added to pgadmin, server 1 is a hosted db on Heroku the other is local to the server
I want to add the Heroku db as foreign tables to the local db
can I link these 2 servers? as so far I have failed
I have the fdw extension setup locally, and I am able to use it across three local dbs ok, do I need the extension also setup on the Heroku db?
I have setup the fdw server as
host localhost --- should this be the Heroku host path? or is this ok since I have added - --server local? I get fsrvoption error if I add actual host path
dbname dbname
port 5432
and then setup user
user username
password password
any help appreciated!
mal

You don't have to set up anything on the remote server to access it via foreign data wrapper; the foreign data wrapper will access the remote database as a regular database client.
Once you hace set up postgres_fdw correctly, you can use it to access tables on the remote server just as if they were local tables. A foreign table is a bit like a view in some respects.
Maybe it helps to understand the objects involved:
The foreign data wrapper encapsulates the code to access the remote data source (PostgreSQL client).
The foreign server wraps the connect string for the remote database.
The user mapping contains the credentials for a user to access the foreign server.
The foreign table describes a table on the remote server.
You can use the command IMPORT FOREIGN SCHEMA to automatically define foreign tables for all (or part) of the tables in a schema on the remote server.
Once the foreign tables are defined correctly, you can use them in SQL statements just like local tables, but you will actually read and write data on the remote server.
After some debugging, the problem turned out to be this:
Creating the foreign server using pgAdmin caused a strange error with fsrvoption (running CREATE SERVER via the query tool worked).
Creating the foreign server with localhost rather than the correct server address unsurprisingly gave the error that the database doesn't exist.

Related

Postgres FDW - Triggers to update local schema when remote schema has a DML

I have a client website that has a Postgres backend which I consider as my remote database. I use FDWs to connect from my local database to the remote DB.
So what I have is :
Remote DB ->remote Schema -> Has a table called remote_work_packages
On my database, this would after I create an FDW server -
Local DB -> Local schema -> foreign table foreign_work_packages
Now as I said the client website connects to the Remote DB , and whenever there is an INSERT, UPDATE or DELETE into remote_work_packages, then I want some functions to be triggered on my local schema.
Please note , I do not have access to create a trigger on the Remote table.
Is there a way this can be achieved. I know since release 9.4, that it is possible to create triggers on foreign tables. But there is no way I could find out how to track activity on the remote itself. Is there a way to track staying on the local DB. Please advise.

Use postgres_fdw user mapping with AWS IAM role?

In order to execute a query on a local PostgreSQL database (v10.14; Amazon RDS) that fetches data from a remote database, also PostgreSQL (RDS), we're using the foreign data wrapper (FDW) postgres_fdw server and user mapping. Currently the user mapping contains user/password credentials for the remote RDS, stored with the user mapping in the local RDS. To become more secure we would like to get rid of the stored credentials.
One way of doing so that we could imagine is using the IAM role of the local RDS to connect to the remote RDS. The question is, is that possible with postgres_fdw, are there maybe other FDWs with the same feature set as postgres_fdw and allow IAM authentication?
Are there other ways to set up an FDW like this without storing the credentials inside the local RDS?

Connect to Hive database tables using postgresql-fdw from postgresql server

Cant connect from postgresql server to hive databases on remote server.
Tried using the following queries but doenst work. Connection established but no response back from hive server.
--create extension postgres_fdw;
DROP USER MAPPING IF EXISTS FOR CURRENT_USER SERVER data_db;
drop server data_db;
create server data_db
foreign data wrapper postgres_fdw
options (host 'net.com' , port 'hiveport' , SSLMODE 'allow', dbname 'datah');
create user mapping for current_user
server data_db
options(user 'user', password 'password');
drop schema app;
create schema app;
import foreign schema public
from server data_db
into app;
The result was unknown expecting authentication request from the server (SQL state 08001).
I would like to be able to establish a connection to the hive database clusters using the fdw from postgresql to import selective data.
Thank you very much in advance and best regards!
postgres_fdw is for connecting to other PostgreSQL instances. Hive doesn't use the same wire protocol as PostgreSQL does, so surely postgres_fdw would not be expected to connect to it successfully. Perhaps you can try https://github.com/youngwookim/hive-fdw-for-postgresql or https://sourceforge.net/projects/hadoopfdw/.

How to add a remote Postgresql db(linked server) to a Postgresql db?

I have a postgresql db at home and one on the cloud. I'd like to add my home db to the cloud db so I can query easily between databases. How can this be done? Without using dblink http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html
My home db will use a dynamic ip provider (can I add a dynamic ip address such as myhomedb.dedyn.io to postgresql settings?)
I'm stating all this in case there are any issues. My home db will only be used to update massive amount of data but isn't mission critical (as we know cloud computing isn't cheap).
Thanks in advance.
Looks like postgres-fdw is the way to go: https://www.postgresql.org/docs/current/postgres-fdw.html
First install the extension:
CREATE EXTENSION postgres_fdw;
Then create a foreign server using CREATE SERVER. In this example we
wish to connect to a PostgreSQL server on host 192.83.123.89
listening on port 5432. The database to which the connection is made
is named foreign_db on the remote server:
CREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db');
A user mapping, defined with CREATE USER MAPPING, is needed as well
to identify the role that will be used on the remote server:
CREATE USER MAPPING FOR local_user
SERVER foreign_server
OPTIONS (user 'foreign_user', password 'password');
Now it is possible to create a foreign table with CREATE FOREIGN
TABLE. In this example we wish to access the table named
some_schema.some_table on the remote server. The local name for it
will be foreign_table:
CREATE FOREIGN TABLE foreign_table (
id integer NOT NULL,
data text
)
SERVER foreign_server
OPTIONS (schema_name 'some_schema', table_name 'some_table');
It's essential that the data types and other properties of the columns
declared in CREATE FOREIGN TABLE match the actual remote table.
Column names must match as well, unless you attach column_name options
to the individual columns to show how they are named in the remote
table. In many cases, use of IMPORT FOREIGN SCHEMA is preferable to
constructing foreign table definitions manually.

Query two different postgres databases stored in different servers

I have two POSTGRES databases stored in different servers.
The "Firstdb" is version 9.2 and it is stored in a LAN server, port 5432.
The "Seconddb" is version 10 and it is stored as localhost to my PC, port 5432.
I have access to both of them through pgAdmin4 version 2.0.
I would like to run query between those two databases to compare data.
Any ideas about how this can be done?
Thank you all for your time.
For running federated queries I use most of the time postgres_fdw, which creates a foreign table in the source database. Quite handy but has its caveats when dealing with joins.
An example:
CREATE SERVER my_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'target.host.com', port '5432', dbname 'targetdb');
CREATE USER MAPPING FOR postgres SERVER my_server OPTIONS (user 'postgres');
CREATE FOREIGN TABLE my_foreign_table (
id INT,
desc TEXT
)
SERVER my_server OPTIONS (schema_name 'public', table_name 'target_table');
EDIT based on the comments:
Keep in mind that the source database, as any other application, needs access to the target database and it has to be described at the pg_hba.conf:
host yourdb youruser 0.0.0.0 md5
Another approach is using dblink, which does not create a foreign table but enables you to fire queries directly to the target database and retrieve the result sets just as if it was local.