2 F.15. file_fdw — access data files in the server's file system #
4 The file_fdw module provides the foreign-data wrapper file_fdw, which
5 can be used to access data files in the server's file system, or to
6 execute programs on the server and read their output. The data file or
7 program output must be in a format that can be read by COPY FROM; see
8 COPY for details. Access to data files is currently read-only.
10 A foreign table created using this wrapper can have the following
14 Specifies the file to be read. Relative paths are relative to
15 the data directory. Either filename or program must be
16 specified, but not both.
19 Specifies the command to be executed. The standard output of
20 this command will be read as though COPY FROM PROGRAM were used.
21 Either program or filename must be specified, but not both.
24 Specifies the data format, the same as COPY's FORMAT option.
27 Specifies whether the data has a header line, the same as COPY's
31 Specifies the data delimiter character, the same as COPY's
35 Specifies the data quote character, the same as COPY's QUOTE
39 Specifies the data escape character, the same as COPY's ESCAPE
43 Specifies the data null string, the same as COPY's NULL option.
46 Specifies the data encoding, the same as COPY's ENCODING option.
49 Specifies how to behave when encountering an error converting a
50 column's input value into its data type, the same as COPY's
54 Specifies the maximum number of errors tolerated while
55 converting a column's input value to its data type, the same as
56 COPY's REJECT_LIMIT option.
59 Specifies the amount of messages emitted by file_fdw, the same
60 as COPY's LOG_VERBOSITY option.
62 Note that while COPY allows options such as HEADER to be specified
63 without a corresponding value, the foreign table option syntax requires
64 a value to be present in all cases. To activate COPY options typically
65 written without a value, you can pass the value TRUE, since all such
68 A column of a foreign table created using this wrapper can have the
72 This is a Boolean option. If true, it specifies that values of
73 the column should not be matched against the null string (that
74 is, the table-level null option). This has the same effect as
75 listing the column in COPY's FORCE_NOT_NULL option.
78 This is a Boolean option. If true, it specifies that values of
79 the column which match the null string are returned as NULL even
80 if the value is quoted. Without this option, only unquoted
81 values matching the null string are returned as NULL. This has
82 the same effect as listing the column in COPY's FORCE_NULL
85 COPY's FORCE_QUOTE option is currently not supported by file_fdw.
87 These options can only be specified for a foreign table or its columns,
88 not in the options of the file_fdw foreign-data wrapper, nor in the
89 options of a server or user mapping using the wrapper.
91 Changing table-level options requires being a superuser or having the
92 privileges of the role pg_read_server_files (to use a filename) or the
93 role pg_execute_server_program (to use a program), for security
94 reasons: only certain users should be able to control which file is
95 read or which program is run. In principle regular users could be
96 allowed to change the other options, but that's not supported at
99 When specifying the program option, keep in mind that the option string
100 is executed by the shell. If you need to pass any arguments to the
101 command that come from an untrusted source, you must be careful to
102 strip or escape any characters that might have special meaning to the
103 shell. For security reasons, it is best to use a fixed command string,
104 or at least avoid passing any user input in it.
106 For a foreign table using file_fdw, EXPLAIN shows the name of the file
107 to be read or program to be run. For a file, unless COSTS OFF is
108 specified, the file size (in bytes) is shown as well.
110 Example F.1. Create a Foreign Table for PostgreSQL CSV Logs
112 One of the obvious uses for file_fdw is to make the PostgreSQL activity
113 log available as a table for querying. To do this, first you must be
114 logging to a CSV file, which here we will call pglog.csv. First,
115 install file_fdw as an extension:
116 CREATE EXTENSION file_fdw;
118 Then create a foreign server:
119 CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;
121 Now you are ready to create the foreign data table. Using the CREATE
122 FOREIGN TABLE command, you will need to define the columns for the
123 table, the CSV file name, and its format:
124 CREATE FOREIGN TABLE pglog (
125 log_time timestamp(3) with time zone,
129 connection_from text,
131 session_line_num bigint,
133 session_start_time timestamp with time zone,
134 virtual_transaction_id text,
135 transaction_id bigint,
142 internal_query_pos integer,
147 application_name text,
152 OPTIONS ( filename 'log/pglog.csv', format 'csv' );
154 That's it — now you can query your log directly. In production, of
155 course, you would need to define some way to deal with log rotation.
157 Example F.2. Create a Foreign Table with an Option on a Column
159 To set the force_null option for a column, use the OPTIONS keyword.
160 CREATE FOREIGN TABLE films (
161 code char(5) NOT NULL,
163 rating text OPTIONS (force_null 'true')
165 OPTIONS ( filename 'films/db.csv', format 'csv' );