CREATE FOREIGN TABLE

An SQL command for creating a foreign table

CREATE FOREIGN TABLE is a DDL command to create a foreign table for a remote data source accessed via a foreign data wrapper (FDW).

CREATE FOREIGN TABLE was added in PostgreSQL 9.1.

Change history

Examples

Basic usage example for CREATE FOREIGN TABLE:

postgres=# CREATE FOREIGN TABLE foo_ft (
             f_id  INT  OPTIONS (column_name 'id') NOT NULL,
             f_val TEXT OPTIONS (column_name 'val'),
             f_bv  BOOL OPTIONS (column_name 'bv')
           )
           SERVER pg_fdw
           OPTIONS (table_name 'foo');
CREATE FOREIGN TABLE

postgres=# \d foo_ft
                      Foreign table "public.foo_ft"
 Column |  Type   | Collation | Nullable | Default |     FDW options     
--------+---------+-----------+----------+---------+---------------------
 f_id   | integer |           | not null |         | (column_name 'id')
 f_val  | text    |           |          |         | (column_name 'val')
 f_bv   | boolean |           |          |         | (column_name 'bv')
Server: pg_fdw
FDW options: (table_name 'foo')

Categories

DDL, Foreign Data Wrapper (FDW), SQL command

See also

ALTER FOREIGN TABLE, DROP FOREIGN TABLE, foreign table