Fetching files with file_fdw

In some cases, it can make sense to read a file from a disk and expose it to PostgreSQL as a table. This is exactly what you can achieve with file_fdw extension. The idea is to have a module that allows you to read data from a disk and query it using SQL.

Installing the module works as expected:

CREATE EXTENSION file_fdw;

In the next step, we create a virtual server:

CREATE SERVER file_server   FOREIGN DATA WRAPPER file_fdw;

The file_server is based on the file_fdw extension foreign data wrapper, which tells PostgreSQL how to access the file.

To expose a file as a table, the following command can be used:

CREATE FOREIGN TABLE t_passwd (      username   text,      passwd     text,      uid         int,      gid         int,      gecos      text,  dir text,  ...

Get Mastering PostgreSQL 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.