
DROP DATABASE IF EXISTS bara;
DROP ROLE IF EXISTS admin;



CREATE DATABASE adam OWNER postgres;

INSERT INTO person VALUES ('Ann', 15), ('Bob', 23), ('Carl', 37);

-- create bara database same as adam
CREATE DATABASE bara OWNER admin TEMPLATE adam;

-- import bara's tables into adam's database using foreign data wrapper
CREATE EXTENSION dblink SCHEMA public;
CREATE EXTENSION postgres_fdw SCHEMA public;

GRANT USAGE ON FOREIGN DATA WRAPPER dblink_fdw TO postgres;
GRANT USAGE ON FOREIGN DATA WRAPPER postgres_fdw TO postgres;


ALTER SERVER foreign_server OWNER TO postgres;


CREATE SCHEMA bara_fdw;
IMPORT FOREIGN SCHEMA public LIMIT TO (person) FROM SERVER foreign_server INTO bara_fdw;


-- PROBLEMATIC PART

--update age on adams database
UPDATE person SET age = 16 WHERE name = 'Ann';

-- want actualize bara's table
UPDATE bara_fdw.person SET (age) = (
    SELECT age
        FROM person
        WHERE bara_fdw.person.name = person.name
);
