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.


Creation of external tables using file_fdw

postgres=# create extension file_fdw ;

postgres=# create server srv_file_fdw foreign data wrapper file_fdw;

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’ );

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s