Create a foreign table pointing to a view

Yes, it is possible!

The following query worked perfectly:

CREATE FOREIGN TABLE facts(name character varying(255))
SERVER my_server 
OPTIONS (table_name 'facts');

Where facts is a view in my_server instead of a table.


Recently I had to do the same thing and here are the steps that worked for me. All these commands are run on the local postgreSQL DB.

CREATE EXTENSION postgres_fdw;
CREATE SERVER remote_server_name
  FOREIGN DATA WRAPPER postgres_fdw
  OPTIONS (host '10.10.10.10', port '5432', dbname 'remote_db_name');

CREATE USER MAPPING FOR local_user_name
  SERVER remote_server_name
  OPTIONS (user 'remote_user', password 'remote_password');

CREATE FOREIGN TABLE local_table_name (
  id NUMERIC NOT NULL,
  row TEXT,
  another_row INTEGER,
  whatever_row TEXT
)
  SERVER remote_server_name
  OPTIONS (schema_name 'public', table_name 'remote_table_name');