2

I need to access tables in a remote Postgres database. I spent a couple hours reading through docs last night, and this is what I've come up with.

-- surely this thing is already written somewhere?
CREATE FUNCTION pgsql_fdw_handler()
    RETURNS fdw_handler
    AS '?????' LANGUAGE C STRICT;

-- surely this thing is already written somewhere?
CREATE FUNCTION pgsql_fdw_validator(text[], oid)
    RETURNS void
    AS '????' LANGUAGE C STRICT;

CREATE FOREIGN DATA WRAPPER pgsql
    HANDLER pgsql_fdw_handler
    VALIDATOR pgsql_fdw_validator;

CREATE SERVER products_remote FOREIGN DATA WRAPPER pgsql OPTIONS
    (host 'localhost', dbname 'product_db', port '1234');

CREATE USER MAPPING FOR CURRENT_USER
    SERVER products_remote;

CREATE FOREIGN TABLE products (
  name text,
  id integer
) SERVER products_remote;

select * from products;

I can't figure out what to do for the handler and validator functions, do they exist somewhere? Do I need to write them myself? (what would that look like?) And would appreciate a validation of the overall approach.

I did find this contrib module, but it's apparently incomplete.

3
  • 2
    The dblink module is better suited for that: postgresql.org/docs/current/static/dblink.html Commented Jul 26, 2012 at 16:01
  • Thanks, that's what I needed. If you make it a real answer, I'll accept it. Commented Jul 26, 2012 at 17:49
  • Or I can just answer myself if that's more convenient. Commented Jul 26, 2012 at 17:50

1 Answer 1

1

First, you need to get the pgsql_fdw code. I could not find a tarball so you are stuck with a git clone. To get it:

git clone git://interdbconnect.git.sourceforge.net/gitroot/interdbconnect/interdbconnect
cd interdbconnect/pgsql_fdw
make USE_PGXS=1
make USE_PGXS=1 install;

Then log into pgsql

CREATE EXTENSION pgsql_fdw;

create foreign data wrapper postgresql  handler pgsql_fdw_handler;
create server lsmb foreign data wrapper postgresql options (host 'localhost', dbname  'mtech_test', port '5432');
create user mapping for current_user server lsmb;
create foreign table defaults (setting_key text, value text) server lsmb;
select * from defaults;

Then this works for me.

I don't know whether pgsql_fdw was included in contrib in 9.2.

Sign up to request clarification or add additional context in comments.

1 Comment

Is this answer still accurate? I think FDW has shipped with POSTGRES since 9.3, but it still asks for a handler function, and the documentation is opaque

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.