file_fdw

访问外部文件的外部数据包装器

概览

扩展包名版本分类许可证语言
file_fdw1.0FDWPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
8980file_fdw-
相关扩展log_fdw wrappers sqlite_fdw aws_s3 pg_bulkload multicorn hdfs_fdw postgres_fdw
下游依赖pg_sqlog

版本

PG18PG17PG16PG15PG14
1.01.01.01.01.0

安装

提示:这是 PostgreSQL 内核自带的 contrib 扩展

CREATE EXTENSION file_fdw;

用法

file_fdw: 平面文件访问的外部数据包装器

创建服务器

CREATE EXTENSION file_fdw;

CREATE SERVER file_server FOREIGN DATA WRAPPER file_fdw;

读取 CSV 文件

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;

读取 PostgreSQL CSV 日志

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

读取程序输出

CREATE FOREIGN TABLE process_list (
  pid text,
  command text
)
SERVER file_server
OPTIONS (program 'ps aux | tail -n +2', format 'text', delimiter ' ');

表选项

选项描述
filename文件路径(相对于数据目录)。除非使用 program,否则必填
program读取其标准输出的 Shell 命令。除非使用 filename,否则必填
format数据格式:csvtextbinary(与 COPY 相同)
header文件是否有标题行时为 true
delimiter列分隔符字符
quote引号字符
escape转义字符
null表示 NULL 值的字符串
encoding数据编码
on_error类型转换时的错误处理
reject_limit最大容忍错误数

列选项

选项描述
force_not_null不将列值与空字符串匹配
force_null将引号值与空字符串匹配并返回 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 是只读的。更改表级选项需要超级用户权限或 pg_read_server_files / pg_execute_server_program 角色。


最后修改 2026-03-12: update extension pages (f579993)