Maciej
Maciej

Reputation: 10835

Postgres - CREATE FOREIGN TABLE based on other table

I'm looking way to simplify CREATE FOREIGN command, so do need to specify all columns and types.

What I'm doing now (using file_fdw server):

CREATE FOREIGN TABLE temp_table_csv 
(id integer, name text)
SERVER csv_log_server
OPTIONS ( filename 'path_to_file.csv', format 'csv');

What I would like to do:

CREATE FOREIGN TABLE temp_table_csv 
(like example_table)
SERVER csv_log_server
OPTIONS ( filename 'path_to_file.csv', format 'csv');

Using LIKE or similar command so Postgres can read structure out of there But it says "like is not supported"

Upvotes: 1

Views: 669

Answers (1)

n0099
n0099

Reputation: 1393

You may use table inheritance with no extra columns to simulate it:

CREATE FOREIGN TABLE temp_table_csv()
INHERITS (example_table)
SERVER csv_log_server
OPTIONS ( filename 'path_to_file.csv', format 'csv')

but this will bring all the side effects of table inheritance, such as SELECT COUNT(*) FROM example_table will now including rows from temp_table_csv, and SELECT COUNT(*) FROM ONLY example_table will not.

Upvotes: 1

Related Questions