file_fdw
foreign-data wrapper for flat file access
file_fdw : foreign-data wrapper for flat file access
Overview
Packages
This is a built-in contrib extension ship with the PostgreSQL kernel
Install
Create this extension with:
CREATE EXTENSION file_fdw;
Usage
file_fdw: Foreign data wrapper for flat file access
Create Server
CREATE EXTENSION file_fdw;
CREATE SERVER file_server FOREIGN DATA WRAPPER file_fdw;
Read a CSV File
CREATE FOREIGN TABLE csv_data (
id integer,
name text,
value numeric
)
SERVER file_server
OPTIONS (filename '/path/to/data.csv', format 'csv', header 'true');
SELECT * FROM csv_data;
Read PostgreSQL CSV Logs
CREATE FOREIGN TABLE pglog (
log_time timestamp(3) with time zone,
user_name text,
database_name text,
process_id integer,
connection_from text,
session_id text,
session_line_num bigint,
command_tag text,
session_start_time timestamp with time zone,
virtual_transaction_id text,
transaction_id bigint,
error_severity text,
sql_state_code text,
message text,
detail text,
hint text,
internal_query text,
internal_query_pos integer,
context text,
query text,
query_pos integer,
location text,
application_name text,
backend_type text,
leader_pid integer,
query_id bigint
)
SERVER file_server
OPTIONS (filename 'log/pglog.csv', format 'csv');
Read Program Output
CREATE FOREIGN TABLE process_list (
pid text,
command text
)
SERVER file_server
OPTIONS (program 'ps aux | tail -n +2', format 'text', delimiter ' ');
Table Options
Column Options
CREATE FOREIGN TABLE films (
code char(5) NOT NULL,
title text NOT NULL,
rating text OPTIONS (force_null 'true')
)
SERVER file_server
OPTIONS (filename '/data/films.csv', format 'csv');
file_fdw is read-only. Changing table-level options requires superuser privileges or the pg_read_server_files / pg_execute_server_program role.