file_fdw

foreign-data wrapper for flat file access

Overview

PackageVersionCategoryLicenseLanguage
file_fdw1.0FDWPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
8980file_fdwNoYesNoYesNoNo-
Relatedlog_fdw wrappers sqlite_fdw aws_s3 pg_bulkload multicorn hdfs_fdw postgres_fdw
Depended Bypg_sqlog

Version

PG18PG17PG16PG15PG14
1.01.01.01.01.0

Install

Note: This is a built-in contrib extension of PostgreSQL

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

OptionDescription
filenameFile path (relative to data directory). Required unless program is used
programShell command whose stdout is read. Required unless filename is used
formatData format: csv, text, or binary (same as COPY)
headertrue if file has a header row
delimiterColumn delimiter character
quoteQuote character
escapeEscape character
nullString representing NULL values
encodingData encoding
on_errorError handling during type conversion
reject_limitMaximum tolerated errors

Column Options

OptionDescription
force_not_nullDo not match column values against the null string
force_nullMatch quoted values against the null string and return NULL
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.


Last Modified 2026-03-12: add pg extension catalog (95749bf)