External tables in PostgreSQL
External tables is a beautiful concept by which we can access the data from files (csv or other formats which are supported by PostgreSQL COPY command) directly without loading the content into the database. We use file_fdw extension to create the external tables. Below is the procedure to do the same.
I have a file student.csv with below content.
12,Srinivas,HYD
13,Rama,GNT
Creation of external tables using file_fdw
postgres=# create extension file_fdw ;
CREATE EXTENSION
postgres=# create server srv_file_fdw foreign data wrapper file_fdw;
CREATE SERVER
postgres=# create foreign table student
postgres-# (
postgres(# id int,
postgres(# name varchar(100),
postgres(# address varchar(50)
postgres(# )
postgres-# server srv_file_fdw
postgres-# options ( filename ‘/tmp/student.csv’, format ‘csv’ );
CREATE FOREIGN TABLE
postgres=# select * from student ;
id | name | address
—-+———-+———
12 | Srinivas | HYD
13 | Rama | GNT
(2 rows)
The foreign table is read-only , you can not perform the updates on the table.
Now you can use this table to copy the data to another table or for select queries.