1 <!-- doc/src/sgml/file-fdw.sgml -->
3 <sect1 id=
"file-fdw" xreflabel=
"file_fdw">
4 <title>file_fdw
— access data files in the server's file system
</title>
6 <indexterm zone=
"file-fdw">
7 <primary>file_fdw
</primary>
11 The
<filename>file_fdw
</filename> module provides the foreign-data wrapper
12 <function>file_fdw
</function>, which can be used to access data
13 files in the server's file system, or to execute programs on the server
14 and read their output. The data file or program output must be in a format
15 that can be read by
<command>COPY FROM
</command>;
16 see
<xref linkend=
"sql-copy"/> for details.
17 Access to data files is currently read-only.
21 A foreign table created using this wrapper can have the following options:
27 <term><literal>filename
</literal></term>
31 Specifies the file to be read. Relative paths are relative to the
33 Either
<literal>filename
</literal> or
<literal>program
</literal> must be
34 specified, but not both.
40 <term><literal>program
</literal></term>
44 Specifies the command to be executed. The standard output of this
45 command will be read as though
<command>COPY FROM PROGRAM
</command> were used.
46 Either
<literal>program
</literal> or
<literal>filename
</literal> must be
47 specified, but not both.
53 <term><literal>format
</literal></term>
57 Specifies the data format,
58 the same as
<command>COPY
</command>'s
<literal>FORMAT
</literal> option.
64 <term><literal>header
</literal></term>
68 Specifies whether the data has a header line,
69 the same as
<command>COPY
</command>'s
<literal>HEADER
</literal> option.
75 <term><literal>delimiter
</literal></term>
79 Specifies the data delimiter character,
80 the same as
<command>COPY
</command>'s
<literal>DELIMITER
</literal> option.
86 <term><literal>quote
</literal></term>
90 Specifies the data quote character,
91 the same as
<command>COPY
</command>'s
<literal>QUOTE
</literal> option.
97 <term><literal>escape
</literal></term>
101 Specifies the data escape character,
102 the same as
<command>COPY
</command>'s
<literal>ESCAPE
</literal> option.
108 <term><literal>null
</literal></term>
112 Specifies the data null string,
113 the same as
<command>COPY
</command>'s
<literal>NULL
</literal> option.
119 <term><literal>encoding
</literal></term>
123 Specifies the data encoding,
124 the same as
<command>COPY
</command>'s
<literal>ENCODING
</literal> option.
130 <term><literal>on_error
</literal></term>
134 Specifies how to behave when encountering an error converting a column's
135 input value into its data type,
136 the same as
<command>COPY
</command>'s
<literal>ON_ERROR
</literal> option.
142 <term><literal>reject_limit
</literal></term>
146 Specifies the maximum number of errors tolerated while converting a column's
147 input value to its data type, the same as
<command>COPY
</command>'s
148 <literal>REJECT_LIMIT
</literal> option.
154 <term><literal>log_verbosity
</literal></term>
158 Specifies the amount of messages emitted by
<literal>file_fdw
</literal>,
159 the same as
<command>COPY
</command>'s
<literal>LOG_VERBOSITY
</literal> option.
167 Note that while
<command>COPY
</command> allows options such as
<literal>HEADER
</literal>
168 to be specified without a corresponding value, the foreign table option
169 syntax requires a value to be present in all cases. To activate
170 <command>COPY
</command> options typically written without a value, you can pass
171 the value TRUE, since all such options are Booleans.
175 A column of a foreign table created using this wrapper can have the
182 <term><literal>force_not_null
</literal></term>
186 This is a Boolean option. If true, it specifies that values of the
187 column should not be matched against the null string (that is, the
188 table-level
<literal>null
</literal> option). This has the same effect
189 as listing the column in
<command>COPY
</command>'s
190 <literal>FORCE_NOT_NULL
</literal> option.
196 <term><literal>force_null
</literal></term>
200 This is a Boolean option. If true, it specifies that values of the
201 column which match the null string are returned as
<literal>NULL
</literal>
202 even if the value is quoted. Without this option, only unquoted
203 values matching the null string are returned as
<literal>NULL
</literal>.
204 This has the same effect as listing the column in
205 <command>COPY
</command>'s
<literal>FORCE_NULL
</literal> option.
213 <command>COPY
</command>'s
<literal>FORCE_QUOTE
</literal> option is
214 currently not supported by
<literal>file_fdw
</literal>.
218 These options can only be specified for a foreign table or its columns, not
219 in the options of the
<literal>file_fdw
</literal> foreign-data wrapper, nor in the
220 options of a server or user mapping using the wrapper.
224 Changing table-level options requires being a superuser or having the privileges
225 of the role
<literal>pg_read_server_files
</literal> (to use a filename) or
226 the role
<literal>pg_execute_server_program
</literal> (to use a program),
227 for security reasons: only certain users should be able to control which file is
228 read or which program is run. In principle regular users could be allowed to
229 change the other options, but that's not supported at present.
233 When specifying the
<literal>program
</literal> option, keep in mind that the option
234 string is executed by the shell. If you need to pass any arguments to the
235 command that come from an untrusted source, you must be careful to strip or
236 escape any characters that might have special meaning to the shell.
237 For security reasons, it is best to use a fixed command string, or at least
238 avoid passing any user input in it.
242 For a foreign table using
<literal>file_fdw
</literal>,
<command>EXPLAIN
</command> shows
243 the name of the file to be read or program to be run.
244 For a file, unless
<literal>COSTS OFF
</literal> is
245 specified, the file size (in bytes) is shown as well.
249 <title>Create a Foreign Table for PostgreSQL CSV Logs
</title>
252 One of the obvious uses for
<literal>file_fdw
</literal> is to make
253 the PostgreSQL activity log available as a table for querying. To
254 do this, first you must be
<link
255 linkend=
"runtime-config-logging-csvlog">logging to a CSV file,
</link>
257 will call
<literal>pglog.csv
</literal>. First, install
<literal>file_fdw
</literal>
262 CREATE EXTENSION file_fdw;
266 Then create a foreign server:
269 CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;
274 Now you are ready to create the foreign data table. Using the
275 <command>CREATE FOREIGN TABLE
</command> command, you will need to define
276 the columns for the table, the CSV file name, and its format:
279 CREATE FOREIGN TABLE pglog (
280 log_time timestamp(
3) with time zone,
284 connection_from text,
286 session_line_num bigint,
288 session_start_time timestamp with time zone,
289 virtual_transaction_id text,
290 transaction_id bigint,
297 internal_query_pos integer,
302 application_name text,
307 OPTIONS ( filename 'log/pglog.csv', format 'csv' );
312 That's it
— now you can query your log directly. In production, of
313 course, you would need to define some way to deal with log rotation.
318 <title>Create a Foreign Table with an Option on a Column
</title>
320 To set the
<literal>force_null
</literal> option for a column, use the
321 <literal>OPTIONS
</literal> keyword.
324 CREATE FOREIGN TABLE films (
325 code char(
5) NOT NULL,
327 rating text OPTIONS (force_null 'true')
329 OPTIONS ( filename 'films/db.csv', format 'csv' );