Search code examples
postgresqlproxyforeign-data-wrapper

Use foreign data wrapper for forwarding


The following architecture: Server A has access to Server B, Server B has access to Server C, but Server A has no direct access to Server C.

I can create a foreign server on Server B like this:

CREATE SERVER foreign_server_b_to_c
    FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS (dbname 'db_on_server_c', host 'serverc', port '5432', fetch_size '100000');

I can create a foreign server on Server A like this:

CREATE SERVER foreign_server_a_to_b
    FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS (dbname 'db_on_server_b', host 'serverb', port '5432', fetch_size '100000');

Is there a possible way to us the foreign data wrapper on Server B as a proxy for direct access from Server A to Server C?

I prefer a solution within the database architecture, but might consider a solution outside of this in case its feasible.


Solution

  • Yes, that is possible. Define a foreign table on B that accesses a table on C, and define a foreign table on A that accesses the foreign table on B. It is straightforward.