Search code examples
sqlpostgresqlpostgres-fdw

What am I doing here while updating a table in a remote db using postgres_fdw?


Here is how I am doing it:

CREATE EXTENSION IF NOT EXISTS postgres_fdw;
    DROP SERVER IF EXISTS myserver CASCADE;
    CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '10.1.1.1', dbname 'mydb', port '5432');
    DROP USER MAPPING IF EXISTS FOR user SERVER myserver;

        CREATE USER MAPPING FOR user
                SERVER myserver
                OPTIONS (user 'user', password 'password');

        CREATE FOREIGN TABLE IF NOT EXISTS foriegnemployee(
         id int,
        name text,
        is_done boolean
        )
          SERVER myserver
          OPTIONS (schema_name 'myschema', table_name 'employee');

When I run the following query, it says table mydb.employee does not not exist:

Update foriegnemployee set is_done=true where id in (select id from sometable);

sometable is a local table. employee table is in a remote db


Solution

  • I tried to mockup your tables:

    t=# create database b;
    CREATE DATABASE
    t=# \c b
    You are now connected to database "b" as user "vao".
    b=#  create table employee (is_done boolean, user_id int);
    CREATE TABLE
    b=# insert into employee select false,1;
    INSERT 0 1
    b=# \c t
    You are now connected to database "t" as user "vao".
    t=# create extension postgres_fdw;
    CREATE EXTENSION
    t=# create server b foreign data wrapper postgres_fdw options (dbname 'b');
    CREATE SERVER
    t=# create user mapping for vao server b;
    CREATE USER MAPPING
    t=# create foreign table ftb (is_done boolean, user_id int) server b options (table_name 'employee');
    CREATE FOREIGN TABLE
    t=# Update ftb set is_done=true where user_id in (select user_id from employee);
    UPDATE 1
    

    code works. Either server or table have wrong options I suppose