]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/copy.sgml
Doc: Fix various inconsistencies
[postgresql] / doc / src / sgml / ref / copy.sgml
1 <!--
2 doc/src/sgml/ref/copy.sgml
3 PostgreSQL documentation
4 -->
5
6
7 <refentry id="sql-copy">
8  <indexterm zone="sql-copy">
9   <primary>COPY</primary>
10  </indexterm>
11
12  <refmeta>
13   <refentrytitle>COPY</refentrytitle>
14   <manvolnum>7</manvolnum>
15   <refmiscinfo>SQL - Language Statements</refmiscinfo>
16  </refmeta>
17
18  <refnamediv>
19   <refname>COPY</refname>
20   <refpurpose>copy data between a file and a table</refpurpose>
21  </refnamediv>
22
23  <refsynopsisdiv>
24 <synopsis>
25 COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
26     FROM { '<replaceable class="parameter">filename</replaceable>' | PROGRAM '<replaceable class="parameter">command</replaceable>' | STDIN }
27     [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
28     [ WHERE <replaceable class="parameter">condition</replaceable> ]
29
30 COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
31     TO { '<replaceable class="parameter">filename</replaceable>' | PROGRAM '<replaceable class="parameter">command</replaceable>' | STDOUT }
32     [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
33
34 <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
35
36     FORMAT <replaceable class="parameter">format_name</replaceable>
37     FREEZE [ <replaceable class="parameter">boolean</replaceable> ]
38     DELIMITER '<replaceable class="parameter">delimiter_character</replaceable>'
39     NULL '<replaceable class="parameter">null_string</replaceable>'
40     HEADER [ <replaceable class="parameter">boolean</replaceable> ]
41     QUOTE '<replaceable class="parameter">quote_character</replaceable>'
42     ESCAPE '<replaceable class="parameter">escape_character</replaceable>'
43     FORCE_QUOTE { ( <replaceable class="parameter">column_name</replaceable> [, ...] ) | * }
44     FORCE_NOT_NULL ( <replaceable class="parameter">column_name</replaceable> [, ...] )
45     FORCE_NULL ( <replaceable class="parameter">column_name</replaceable> [, ...] )
46     ENCODING '<replaceable class="parameter">encoding_name</replaceable>'
47 </synopsis>
48  </refsynopsisdiv>
49
50  <refsect1>
51   <title>Description</title>
52
53   <para>
54    <command>COPY</command> moves data between
55    <productname>PostgreSQL</productname> tables and standard file-system
56    files. <command>COPY TO</command> copies the contents of a table
57    <emphasis>to</emphasis> a file, while <command>COPY FROM</command> copies
58    data <emphasis>from</emphasis> a file to a table (appending the data to
59    whatever is in the table already).  <command>COPY TO</command>
60    can also copy the results of a <command>SELECT</command> query.
61   </para>
62
63   <para>
64    If a list of columns is specified, <command>COPY</command> will
65    only copy the data in the specified columns to or from the file.
66    If there are any columns in the table that are not in the column list,
67    <command>COPY FROM</command> will insert the default values for
68    those columns.
69   </para>
70
71   <para>
72    <command>COPY</command> with a file name instructs the
73    <productname>PostgreSQL</productname> server to directly read from
74    or write to a file. The file must be accessible by the
75    <productname>PostgreSQL</productname> user (the user ID the server
76    runs as) and the name must be specified from the viewpoint of the
77    server. When <literal>PROGRAM</literal> is specified, the server
78    executes the given command and reads from the standard output of the
79    program, or writes to the standard input of the program. The command
80    must be specified from the viewpoint of the server, and be executable
81    by the <productname>PostgreSQL</productname> user.  When
82    <literal>STDIN</literal> or <literal>STDOUT</literal> is
83    specified, data is transmitted via the connection between the
84    client and the server.
85   </para>
86  </refsect1>
87
88  <refsect1>
89   <title>Parameters</title>
90
91   <variablelist>
92    <varlistentry>
93     <term><replaceable class="parameter">table_name</replaceable></term>
94     <listitem>
95      <para>
96       The name (optionally schema-qualified) of an existing table.
97      </para>
98     </listitem>
99    </varlistentry>
100
101    <varlistentry>
102     <term><replaceable class="parameter">column_name</replaceable></term>
103      <listitem>
104      <para>
105       An optional list of columns to be copied.  If no column list is
106       specified, all columns of the table except generated columns will be
107       copied.
108      </para>
109     </listitem>
110    </varlistentry>
111
112    <varlistentry>
113     <term><replaceable class="parameter">query</replaceable></term>
114     <listitem>
115      <para>
116       A <xref linkend="sql-select"/>, <xref linkend="sql-values"/>,
117       <xref linkend="sql-insert"/>, <xref linkend="sql-update"/> or
118       <xref linkend="sql-delete"/> command whose results are to be
119       copied.  Note that parentheses are required around the query.
120      </para>
121      <para>
122       For <command>INSERT</command>, <command>UPDATE</command> and
123       <command>DELETE</command> queries a RETURNING clause must be provided,
124       and the target relation must not have a conditional rule, nor
125       an <literal>ALSO</literal> rule, nor an <literal>INSTEAD</literal> rule
126       that expands to multiple statements.
127      </para>
128     </listitem>
129    </varlistentry>
130
131    <varlistentry>
132     <term><replaceable class="parameter">filename</replaceable></term>
133     <listitem>
134      <para>
135       The path name of the input or output file.  An input file name can be
136       an absolute or relative path, but an output file name must be an absolute
137       path.  Windows users might need to use an <literal>E''</literal> string and
138       double any backslashes used in the path name.
139      </para>
140     </listitem>
141    </varlistentry>
142
143    <varlistentry>
144     <term><literal>PROGRAM</literal></term>
145     <listitem>
146      <para>
147       A command to execute. In <command>COPY FROM</command>, the input is
148       read from standard output of the command, and in <command>COPY TO</command>,
149       the output is written to the standard input of the command.
150      </para>
151      <para>
152       Note that the command is invoked by the shell, so if you need to pass
153       any arguments to shell command that come from an untrusted source, you
154       must be careful to strip or escape any special characters that might
155       have a special meaning for the shell. For security reasons, it is best
156       to use a fixed command string, or at least avoid passing any user input
157       in it.
158      </para>
159     </listitem>
160    </varlistentry>
161
162    <varlistentry>
163     <term><literal>STDIN</literal></term>
164     <listitem>
165      <para>
166       Specifies that input comes from the client application.
167      </para>
168     </listitem>
169    </varlistentry>
170
171    <varlistentry>
172     <term><literal>STDOUT</literal></term>
173     <listitem>
174      <para>
175       Specifies that output goes to the client application.
176      </para>
177     </listitem>
178    </varlistentry>
179
180    <varlistentry>
181     <term><replaceable class="parameter">boolean</replaceable></term>
182     <listitem>
183      <para>
184       Specifies whether the selected option should be turned on or off.
185       You can write <literal>TRUE</literal>, <literal>ON</literal>, or
186       <literal>1</literal> to enable the option, and <literal>FALSE</literal>,
187       <literal>OFF</literal>, or <literal>0</literal> to disable it.  The
188       <replaceable class="parameter">boolean</replaceable> value can also
189       be omitted, in which case <literal>TRUE</literal> is assumed.
190      </para>
191     </listitem>
192    </varlistentry>
193
194    <varlistentry>
195     <term><literal>FORMAT</literal></term>
196     <listitem>
197      <para>
198       Selects the data format to be read or written:
199       <literal>text</literal>,
200       <literal>csv</literal> (Comma Separated Values),
201       or <literal>binary</literal>.
202       The default is <literal>text</literal>.
203      </para>
204     </listitem>
205    </varlistentry>
206
207    <varlistentry>
208     <term><literal>FREEZE</literal></term>
209     <listitem>
210      <para>
211       Requests copying the data with rows already frozen, just as they
212       would be after running the <command>VACUUM FREEZE</command> command.
213       This is intended as a performance option for initial data loading.
214       Rows will be frozen only if the table being loaded has been created
215       or truncated in the current subtransaction, there are no cursors
216       open and there are no older snapshots held by this transaction.  It is
217       currently not possible to perform a <command>COPY FREEZE</command> on
218       a partitioned table.
219      </para>
220      <para>
221       Note that all other sessions will immediately be able to see the data
222       once it has been successfully loaded. This violates the normal rules
223       of MVCC visibility and users specifying should be aware of the
224       potential problems this might cause.
225      </para>
226     </listitem>
227    </varlistentry>
228
229    <varlistentry>
230     <term><literal>DELIMITER</literal></term>
231     <listitem>
232      <para>
233       Specifies the character that separates columns within each row
234       (line) of the file.  The default is a tab character in text format,
235       a comma in <literal>CSV</literal> format.
236       This must be a single one-byte character.
237       This option is not allowed when using <literal>binary</literal> format.
238      </para>
239     </listitem>
240    </varlistentry>
241
242    <varlistentry>
243     <term><literal>NULL</literal></term>
244     <listitem>
245      <para>
246       Specifies the string that represents a null value. The default is
247       <literal>\N</literal> (backslash-N) in text format, and an unquoted empty
248       string in <literal>CSV</literal> format. You might prefer an
249       empty string even in text format for cases where you don't want to
250       distinguish nulls from empty strings.
251       This option is not allowed when using <literal>binary</literal> format.
252      </para>
253
254      <note>
255       <para>
256        When using <command>COPY FROM</command>, any data item that matches
257        this string will be stored as a null value, so you should make
258        sure that you use the same string as you used with
259        <command>COPY TO</command>.
260       </para>
261      </note>
262
263     </listitem>
264    </varlistentry>
265
266    <varlistentry>
267     <term><literal>HEADER</literal></term>
268     <listitem>
269      <para>
270       Specifies that the file contains a header line with the names of each
271       column in the file.  On output, the first line contains the column
272       names from the table, and on input, the first line is ignored.
273       This option is allowed only when using <literal>CSV</literal> format.
274      </para>
275     </listitem>
276    </varlistentry>
277
278    <varlistentry>
279     <term><literal>QUOTE</literal></term>
280     <listitem>
281      <para>
282       Specifies the quoting character to be used when a data value is quoted.
283       The default is double-quote.
284       This must be a single one-byte character.
285       This option is allowed only when using <literal>CSV</literal> format.
286      </para>
287     </listitem>
288    </varlistentry>
289
290    <varlistentry>
291     <term><literal>ESCAPE</literal></term>
292     <listitem>
293      <para>
294       Specifies the character that should appear before a
295       data character that matches the <literal>QUOTE</literal> value.
296       The default is the same as the <literal>QUOTE</literal> value (so that
297       the quoting character is doubled if it appears in the data).
298       This must be a single one-byte character.
299       This option is allowed only when using <literal>CSV</literal> format.
300      </para>
301     </listitem>
302    </varlistentry>
303
304    <varlistentry>
305     <term><literal>FORCE_QUOTE</literal></term>
306     <listitem>
307      <para>
308       Forces quoting to be
309       used for all non-<literal>NULL</literal> values in each specified column.
310       <literal>NULL</literal> output is never quoted. If <literal>*</literal> is specified,
311       non-<literal>NULL</literal> values will be quoted in all columns.
312       This option is allowed only in <command>COPY TO</command>, and only when
313       using <literal>CSV</literal> format.
314      </para>
315     </listitem>
316    </varlistentry>
317
318    <varlistentry>
319     <term><literal>FORCE_NOT_NULL</literal></term>
320     <listitem>
321      <para>
322       Do not match the specified columns' values against the null string.
323       In the default case where the null string is empty, this means that
324       empty values will be read as zero-length strings rather than nulls,
325       even when they are not quoted.
326       This option is allowed only in <command>COPY FROM</command>, and only when
327       using <literal>CSV</literal> format.
328      </para>
329     </listitem>
330    </varlistentry>
331
332    <varlistentry>
333     <term><literal>FORCE_NULL</literal></term>
334     <listitem>
335      <para>
336       Match the specified columns' values against the null string, even
337       if it has been quoted, and if a match is found set the value to
338       <literal>NULL</literal>. In the default case where the null string is empty,
339       this converts a quoted empty string into NULL.
340       This option is allowed only in <command>COPY FROM</command>, and only when
341       using <literal>CSV</literal> format.
342      </para>
343     </listitem>
344    </varlistentry>
345
346    <varlistentry>
347     <term><literal>ENCODING</literal></term>
348     <listitem>
349      <para>
350       Specifies that the file is encoded in the <replaceable
351       class="parameter">encoding_name</replaceable>.  If this option is
352       omitted, the current client encoding is used. See the Notes below
353       for more details.
354      </para>
355     </listitem>
356    </varlistentry>
357
358    <varlistentry>
359     <term><literal>WHERE</literal></term>
360     <listitem>
361    <para>
362     The optional <literal>WHERE</literal> clause has the general form
363 <synopsis>
364 WHERE <replaceable class="parameter">condition</replaceable>
365 </synopsis>
366     where <replaceable class="parameter">condition</replaceable> is
367     any expression that evaluates to a result of type
368     <type>boolean</type>.  Any row that does not satisfy this
369     condition will not be inserted to the table.  A row satisfies the
370     condition if it returns true when the actual row values are
371     substituted for any variable references.
372    </para>
373
374    <para>
375     Currently, subqueries are not allowed in <literal>WHERE</literal>
376     expressions, and the evaluation does not see any changes made by the
377     <command>COPY</command> itself (this matters when the expression
378     contains calls to <literal>VOLATILE</literal> functions).
379    </para>
380
381     </listitem>
382    </varlistentry>
383
384   </variablelist>
385  </refsect1>
386
387  <refsect1>
388   <title>Outputs</title>
389
390   <para>
391    On successful completion, a <command>COPY</command> command returns a command
392    tag of the form
393 <screen>
394 COPY <replaceable class="parameter">count</replaceable>
395 </screen>
396    The <replaceable class="parameter">count</replaceable> is the number
397    of rows copied.
398   </para>
399
400   <note>
401    <para>
402     <application>psql</application> will print this command tag only if the command
403     was not <literal>COPY ... TO STDOUT</literal>, or the
404     equivalent <application>psql</application> meta-command
405     <literal>\copy ... to stdout</literal>.  This is to prevent confusing the
406     command tag with the data that was just printed.
407    </para>
408   </note>
409  </refsect1>
410
411  <refsect1>
412   <title>Notes</title>
413
414    <para>
415     <command>COPY TO</command> can only be used with plain tables, not
416     with views.  However, you can write <literal>COPY (SELECT * FROM
417     <replaceable class="parameter">viewname</replaceable>) TO ...</literal>
418     to copy the current contents of a view.
419    </para>
420
421    <para>
422     <command>COPY FROM</command> can be used with plain, foreign, or
423     partitioned tables or with views that have
424     <literal>INSTEAD OF INSERT</literal> triggers.
425    </para>
426
427    <para>
428     <command>COPY</command> only deals with the specific table named;
429     it does not copy data to or from child tables.  Thus for example
430     <literal>COPY <replaceable class="parameter">table</replaceable> TO</literal>
431     shows the same data as <literal>SELECT * FROM ONLY <replaceable
432     class="parameter">table</replaceable></literal>.  But <literal>COPY
433     (SELECT * FROM <replaceable class="parameter">table</replaceable>) TO ...</literal>
434     can be used to dump all of the data in an inheritance hierarchy.
435    </para>
436
437    <para>
438     You must have select privilege on the table
439     whose values are read by <command>COPY TO</command>, and
440     insert privilege on the table into which values
441     are inserted by <command>COPY FROM</command>.  It is sufficient
442     to have column privileges on the column(s) listed in the command.
443    </para>
444
445    <para>
446     If row-level security is enabled for the table, the relevant
447     <command>SELECT</command> policies will apply to <literal>COPY
448     <replaceable class="parameter">table</replaceable> TO</literal> statements.
449     Currently, <command>COPY FROM</command> is not supported for tables
450     with row-level security. Use equivalent <command>INSERT</command>
451     statements instead.
452    </para>
453
454    <para>
455     Files named in a <command>COPY</command> command are read or written
456     directly by the server, not by the client application. Therefore,
457     they must reside on or be accessible to the database server machine,
458     not the client. They must be accessible to and readable or writable
459     by the <productname>PostgreSQL</productname> user (the user ID the
460     server runs as), not the client. Similarly,
461     the command specified with <literal>PROGRAM</literal> is executed directly
462     by the server, not by the client application, must be executable by the
463     <productname>PostgreSQL</productname> user.
464     <command>COPY</command> naming a file or command is only allowed to
465     database superusers or users who are granted one of the default roles
466     <literal>pg_read_server_files</literal>,
467     <literal>pg_write_server_files</literal>,
468     or <literal>pg_execute_server_program</literal>, since it allows reading
469     or writing any file or running a program that the server has privileges to
470     access.
471    </para>
472
473    <para>
474     Do not confuse <command>COPY</command> with the
475     <application>psql</application> instruction
476     <command><link linkend="app-psql-meta-commands-copy">\copy</link></command>. <command>\copy</command> invokes
477     <command>COPY FROM STDIN</command> or <command>COPY TO
478     STDOUT</command>, and then fetches/stores the data in a file
479     accessible to the <application>psql</application> client. Thus,
480     file accessibility and access rights depend on the client rather
481     than the server when <command>\copy</command> is used.
482    </para>
483
484    <para>
485     It is recommended that the file name used in <command>COPY</command>
486     always be specified as an absolute path. This is enforced by the
487     server in the case of <command>COPY TO</command>, but for
488     <command>COPY FROM</command> you do have the option of reading from
489     a file specified by a relative path. The path will be interpreted
490     relative to the working directory of the server process (normally
491     the cluster's data directory), not the client's working directory.
492    </para>
493
494    <para>
495     Executing a command with <literal>PROGRAM</literal> might be restricted
496     by the operating system's access control mechanisms, such as SELinux.
497    </para>
498
499    <para>
500     <command>COPY FROM</command> will invoke any triggers and check
501     constraints on the destination table. However, it will not invoke rules.
502    </para>
503
504    <para>
505     For identity columns, the <command>COPY FROM</command> command will always
506     write the column values provided in the input data, like
507     the <command>INSERT</command> option <literal>OVERRIDING SYSTEM
508     VALUE</literal>.
509    </para>
510
511    <para>
512     <command>COPY</command> input and output is affected by
513     <varname>DateStyle</varname>. To ensure portability to other
514     <productname>PostgreSQL</productname> installations that might use
515     non-default <varname>DateStyle</varname> settings,
516     <varname>DateStyle</varname> should be set to <literal>ISO</literal> before
517     using <command>COPY TO</command>.  It is also a good idea to avoid dumping
518     data with <varname>IntervalStyle</varname> set to
519     <literal>sql_standard</literal>, because negative interval values might be
520     misinterpreted by a server that has a different setting for
521     <varname>IntervalStyle</varname>.
522    </para>
523
524    <para>
525     Input data is interpreted according to <literal>ENCODING</literal>
526     option or the current client encoding, and output data is encoded
527     in <literal>ENCODING</literal> or the current client encoding, even
528     if the data does not pass through the client but is read from or
529     written to a file directly by the server.
530    </para>
531
532    <para>
533     <command>COPY</command> stops operation at the first error. This
534     should not lead to problems in the event of a <command>COPY
535     TO</command>, but the target table will already have received
536     earlier rows in a <command>COPY FROM</command>. These rows will not
537     be visible or accessible, but they still occupy disk space. This might
538     amount to a considerable amount of wasted disk space if the failure
539     happened well into a large copy operation. You might wish to invoke
540     <command>VACUUM</command> to recover the wasted space.
541    </para>
542
543    <para>
544     <literal>FORCE_NULL</literal> and <literal>FORCE_NOT_NULL</literal> can be used
545     simultaneously on the same column. This results in converting quoted
546     null strings to null values and unquoted null strings to empty strings.
547    </para>
548
549  </refsect1>
550
551  <refsect1>
552   <title>File Formats</title>
553
554   <refsect2>
555    <title>Text Format</title>
556
557    <para>
558     When the <literal>text</literal> format is used,
559     the data read or written is a text file with one line per table row.
560     Columns in a row are separated by the delimiter character.
561     The column values themselves are strings generated by the
562     output function, or acceptable to the input function, of each
563     attribute's data type.  The specified null string is used in
564     place of columns that are null.
565     <command>COPY FROM</command> will raise an error if any line of the
566     input file contains more or fewer columns than are expected.
567    </para>
568
569    <para>
570     End of data can be represented by a single line containing just
571     backslash-period (<literal>\.</literal>).  An end-of-data marker is
572     not necessary when reading from a file, since the end of file
573     serves perfectly well; it is needed only when copying data to or from
574     client applications using pre-3.0 client protocol.
575    </para>
576
577    <para>
578     Backslash characters (<literal>\</literal>) can be used in the
579     <command>COPY</command> data to quote data characters that might
580     otherwise be taken as row or column delimiters. In particular, the
581     following characters <emphasis>must</emphasis> be preceded by a backslash if
582     they appear as part of a column value: backslash itself,
583     newline, carriage return, and the current delimiter character.
584    </para>
585
586    <para>
587     The specified null string is sent by <command>COPY TO</command> without
588     adding any backslashes; conversely, <command>COPY FROM</command> matches
589     the input against the null string before removing backslashes.  Therefore,
590     a null string such as <literal>\N</literal> cannot be confused with
591     the actual data value <literal>\N</literal> (which would be represented
592     as <literal>\\N</literal>).
593    </para>
594
595    <para>
596     The following special backslash sequences are recognized by
597     <command>COPY FROM</command>:
598
599    <informaltable>
600     <tgroup cols="2">
601      <thead>
602       <row>
603        <entry>Sequence</entry>
604        <entry>Represents</entry>
605       </row>
606      </thead>
607
608      <tbody>
609       <row>
610        <entry><literal>\b</literal></entry>
611        <entry>Backspace (ASCII 8)</entry>
612       </row>
613       <row>
614        <entry><literal>\f</literal></entry>
615        <entry>Form feed (ASCII 12)</entry>
616       </row>
617       <row>
618        <entry><literal>\n</literal></entry>
619        <entry>Newline (ASCII 10)</entry>
620       </row>
621       <row>
622        <entry><literal>\r</literal></entry>
623        <entry>Carriage return (ASCII 13)</entry>
624       </row>
625       <row>
626        <entry><literal>\t</literal></entry>
627        <entry>Tab (ASCII 9)</entry>
628       </row>
629       <row>
630        <entry><literal>\v</literal></entry>
631        <entry>Vertical tab (ASCII 11)</entry>
632       </row>
633       <row>
634        <entry><literal>\</literal><replaceable>digits</replaceable></entry>
635        <entry>Backslash followed by one to three octal digits specifies
636        the character with that numeric code</entry>
637       </row>
638       <row>
639        <entry><literal>\x</literal><replaceable>digits</replaceable></entry>
640        <entry>Backslash <literal>x</literal> followed by one or two hex digits specifies
641        the character with that numeric code</entry>
642       </row>
643      </tbody>
644     </tgroup>
645    </informaltable>
646
647     Presently, <command>COPY TO</command> will never emit an octal or
648     hex-digits backslash sequence, but it does use the other sequences
649     listed above for those control characters.
650    </para>
651
652    <para>
653     Any other backslashed character that is not mentioned in the above table
654     will be taken to represent itself.  However, beware of adding backslashes
655     unnecessarily, since that might accidentally produce a string matching the
656     end-of-data marker (<literal>\.</literal>) or the null string (<literal>\N</literal> by
657     default).  These strings will be recognized before any other backslash
658     processing is done.
659    </para>
660
661    <para>
662     It is strongly recommended that applications generating <command>COPY</command> data convert
663     data newlines and carriage returns to the <literal>\n</literal> and
664     <literal>\r</literal> sequences respectively.  At present it is
665     possible to represent a data carriage return by a backslash and carriage
666     return, and to represent a data newline by a backslash and newline.
667     However, these representations might not be accepted in future releases.
668     They are also highly vulnerable to corruption if the <command>COPY</command> file is
669     transferred across different machines (for example, from Unix to Windows
670     or vice versa).
671    </para>
672
673    <para>
674     <command>COPY TO</command> will terminate each row with a Unix-style
675     newline (<quote><literal>\n</literal></quote>).  Servers running on Microsoft Windows instead
676     output carriage return/newline (<quote><literal>\r\n</literal></quote>), but only for
677     <command>COPY</command> to a server file; for consistency across platforms,
678     <command>COPY TO STDOUT</command> always sends <quote><literal>\n</literal></quote>
679     regardless of server platform.
680     <command>COPY FROM</command> can handle lines ending with newlines,
681     carriage returns, or carriage return/newlines.  To reduce the risk of
682     error due to un-backslashed newlines or carriage returns that were
683     meant as data, <command>COPY FROM</command> will complain if the line
684     endings in the input are not all alike.
685    </para>
686   </refsect2>
687
688   <refsect2>
689    <title>CSV Format</title>
690
691    <para>
692     This format option is used for importing and exporting the Comma
693     Separated Value (<literal>CSV</literal>) file format used by many other
694     programs, such as spreadsheets. Instead of the escaping rules used by
695     <productname>PostgreSQL</productname>'s standard text format, it
696     produces and recognizes the common CSV escaping mechanism.
697    </para>
698
699    <para>
700     The values in each record are separated by the <literal>DELIMITER</literal>
701     character. If the value contains the delimiter character, the
702     <literal>QUOTE</literal> character, the <literal>NULL</literal> string, a carriage
703     return, or line feed character, then the whole value is prefixed and
704     suffixed by the <literal>QUOTE</literal> character, and any occurrence
705     within the value of a <literal>QUOTE</literal> character or the
706     <literal>ESCAPE</literal> character is preceded by the escape character.
707     You can also use <literal>FORCE_QUOTE</literal> to force quotes when outputting
708     non-<literal>NULL</literal> values in specific columns.
709    </para>
710
711    <para>
712     The <literal>CSV</literal> format has no standard way to distinguish a
713     <literal>NULL</literal> value from an empty string.
714     <productname>PostgreSQL</productname>'s <command>COPY</command> handles this by quoting.
715     A <literal>NULL</literal> is output as the <literal>NULL</literal> parameter string
716     and is not quoted, while a non-<literal>NULL</literal> value matching the
717     <literal>NULL</literal> parameter string is quoted.  For example, with the
718     default settings, a <literal>NULL</literal> is written as an unquoted empty
719     string, while an empty string data value is written with double quotes
720     (<literal>""</literal>). Reading values follows similar rules. You can
721     use <literal>FORCE_NOT_NULL</literal> to prevent <literal>NULL</literal> input
722     comparisons for specific columns. You can also use
723     <literal>FORCE_NULL</literal> to convert quoted null string data values to
724     <literal>NULL</literal>.
725    </para>
726
727    <para>
728     Because backslash is not a special character in the <literal>CSV</literal>
729     format, <literal>\.</literal>, the end-of-data marker, could also appear
730     as a data value.  To avoid any misinterpretation, a <literal>\.</literal>
731     data value appearing as a lone entry on a line is automatically
732     quoted on output, and on input, if quoted, is not interpreted as the
733     end-of-data marker.  If you are loading a file created by another
734     application that has a single unquoted column and might have a
735     value of <literal>\.</literal>, you might need to quote that value in the
736     input file.
737    </para>
738
739    <note>
740     <para>
741      In <literal>CSV</literal> format, all characters are significant. A quoted value
742      surrounded by white space, or any characters other than
743      <literal>DELIMITER</literal>, will include those characters. This can cause
744      errors if you import data from a system that pads <literal>CSV</literal>
745      lines with white space out to some fixed width. If such a situation
746      arises you might need to preprocess the <literal>CSV</literal> file to remove
747      the trailing white space, before importing the data into
748      <productname>PostgreSQL</productname>.
749     </para>
750    </note>
751
752    <note>
753     <para>
754      CSV format will both recognize and produce CSV files with quoted
755      values containing embedded carriage returns and line feeds. Thus
756      the files are not strictly one line per table row like text-format
757      files.
758     </para>
759    </note>
760
761    <note>
762     <para>
763      Many programs produce strange and occasionally perverse CSV files,
764      so the file format is more a convention than a standard. Thus you
765      might encounter some files that cannot be imported using this
766      mechanism, and <command>COPY</command> might produce files that other
767      programs cannot process.
768     </para>
769    </note>
770
771   </refsect2>
772
773   <refsect2>
774    <title>Binary Format</title>
775
776    <para>
777     The <literal>binary</literal> format option causes all data to be
778     stored/read as binary format rather than as text.  It is
779     somewhat faster than the text and <literal>CSV</literal> formats,
780     but a binary-format file is less portable across machine architectures and
781     <productname>PostgreSQL</productname> versions.
782     Also, the binary format is very data type specific; for example
783     it will not work to output binary data from a <type>smallint</type> column
784     and read it into an <type>integer</type> column, even though that would work
785     fine in text format.
786    </para>
787
788    <para>
789     The <literal>binary</literal> file format consists
790     of a file header, zero or more tuples containing the row data, and
791     a file trailer.  Headers and data are in network byte order.
792    </para>
793
794    <note>
795     <para>
796      <productname>PostgreSQL</productname> releases before 7.4 used a
797      different binary file format.
798     </para>
799    </note>
800
801    <refsect3>
802     <title>File Header</title>
803
804     <para>
805      The file header consists of 15 bytes of fixed fields, followed
806      by a variable-length header extension area.  The fixed fields are:
807
808     <variablelist>
809      <varlistentry>
810       <term>Signature</term>
811       <listitem>
812        <para>
813 11-byte sequence <literal>PGCOPY\n\377\r\n\0</literal> &mdash; note that the zero byte
814 is a required part of the signature.  (The signature is designed to allow
815 easy identification of files that have been munged by a non-8-bit-clean
816 transfer.  This signature will be changed by end-of-line-translation
817 filters, dropped zero bytes, dropped high bits, or parity changes.)
818        </para>
819       </listitem>
820      </varlistentry>
821
822      <varlistentry>
823       <term>Flags field</term>
824       <listitem>
825        <para>
826 32-bit integer bit mask to denote important aspects of the file format. Bits
827 are numbered from 0 (<acronym>LSB</acronym>) to 31 (<acronym>MSB</acronym>).  Note that
828 this field is stored in network byte order (most significant byte first),
829 as are all the integer fields used in the file format.  Bits
830 16-31 are reserved to denote critical file format issues; a reader
831 should abort if it finds an unexpected bit set in this range. Bits 0-15
832 are reserved to signal backwards-compatible format issues; a reader
833 should simply ignore any unexpected bits set in this range. Currently
834 only one flag bit is defined, and the rest must be zero:
835         <variablelist>
836          <varlistentry>
837           <term>Bit 16</term>
838           <listitem>
839            <para>
840             If 1, OIDs are included in the data; if 0, not. Oid system columns
841             are not supported in <productname>PostgreSQL</productname>
842             anymore, but the format still contains the indicator.
843            </para>
844           </listitem>
845          </varlistentry>
846         </variablelist></para>
847       </listitem>
848      </varlistentry>
849
850      <varlistentry>
851       <term>Header extension area length</term>
852       <listitem>
853        <para>
854 32-bit integer, length in bytes of remainder of header, not including self.
855 Currently, this is zero, and the first tuple follows
856 immediately.  Future changes to the format might allow additional data
857 to be present in the header.  A reader should silently skip over any header
858 extension data it does not know what to do with.
859        </para>
860       </listitem>
861      </varlistentry>
862     </variablelist>
863     </para>
864
865     <para>
866 The header extension area is envisioned to contain a sequence of
867 self-identifying chunks.  The flags field is not intended to tell readers
868 what is in the extension area.  Specific design of header extension contents
869 is left for a later release.
870     </para>
871
872     <para>
873      This design allows for both backwards-compatible header additions (add
874      header extension chunks, or set low-order flag bits) and
875      non-backwards-compatible changes (set high-order flag bits to signal such
876      changes, and add supporting data to the extension area if needed).
877     </para>
878    </refsect3>
879
880    <refsect3>
881     <title>Tuples</title>
882     <para>
883 Each tuple begins with a 16-bit integer count of the number of fields in the
884 tuple.  (Presently, all tuples in a table will have the same count, but that
885 might not always be true.)  Then, repeated for each field in the tuple, there
886 is a 32-bit length word followed by that many bytes of field data.  (The
887 length word does not include itself, and can be zero.)  As a special case,
888 -1 indicates a NULL field value.  No value bytes follow in the NULL case.
889     </para>
890
891     <para>
892 There is no alignment padding or any other extra data between fields.
893     </para>
894
895     <para>
896 Presently, all data values in a binary-format file are
897 assumed to be in binary format (format code one).  It is anticipated that a
898 future extension might add a header field that allows per-column format codes
899 to be specified.
900     </para>
901
902     <para>
903 To determine the appropriate binary format for the actual tuple data you
904 should consult the <productname>PostgreSQL</productname> source, in
905 particular the <function>*send</function> and <function>*recv</function> functions for
906 each column's data type (typically these functions are found in the
907 <filename>src/backend/utils/adt/</filename> directory of the source
908 distribution).
909     </para>
910
911     <para>
912 If OIDs are included in the file, the OID field immediately follows the
913 field-count word.  It is a normal field except that it's not included in the
914 field-count.  Note that oid system columns are not supported in current
915 versions of <productname>PostgreSQL</productname>.
916     </para>
917    </refsect3>
918
919    <refsect3>
920     <title>File Trailer</title>
921
922     <para>
923      The file trailer consists of a 16-bit integer word containing -1.  This
924      is easily distinguished from a tuple's field-count word.
925     </para>
926
927     <para>
928      A reader should report an error if a field-count word is neither -1
929      nor the expected number of columns.  This provides an extra
930      check against somehow getting out of sync with the data.
931     </para>
932    </refsect3>
933   </refsect2>
934  </refsect1>
935
936  <refsect1>
937   <title>Examples</title>
938
939   <para>
940    The following example copies a table to the client
941    using the vertical bar (<literal>|</literal>) as the field delimiter:
942 <programlisting>
943 COPY country TO STDOUT (DELIMITER '|');
944 </programlisting>
945   </para>
946
947   <para>
948    To copy data from a file into the <literal>country</literal> table:
949 <programlisting>
950 COPY country FROM '/usr1/proj/bray/sql/country_data';
951 </programlisting>
952   </para>
953
954   <para>
955    To copy into a file just the countries whose names start with 'A':
956 <programlisting>
957 COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';
958 </programlisting>
959   </para>
960
961   <para>
962    To copy into a compressed file, you can pipe the output through an external
963    compression program:
964 <programlisting>
965 COPY country TO PROGRAM 'gzip > /usr1/proj/bray/sql/country_data.gz';
966 </programlisting>
967   </para>
968
969   <para>
970    Here is a sample of data suitable for copying into a table from
971    <literal>STDIN</literal>:
972 <programlisting>
973 AF      AFGHANISTAN
974 AL      ALBANIA
975 DZ      ALGERIA
976 ZM      ZAMBIA
977 ZW      ZIMBABWE
978 </programlisting>
979    Note that the white space on each line is actually a tab character.
980   </para>
981
982   <para>
983    The following is the same data, output in binary format.
984    The data is shown after filtering through the
985    Unix utility <command>od -c</command>. The table has three columns;
986    the first has type <type>char(2)</type>, the second has type <type>text</type>,
987    and the third has type <type>integer</type>. All the rows have a null value
988    in the third column.
989 <programlisting>
990 0000000   P   G   C   O   P   Y  \n 377  \r  \n  \0  \0  \0  \0  \0  \0
991 0000020  \0  \0  \0  \0 003  \0  \0  \0 002   A   F  \0  \0  \0 013   A
992 0000040   F   G   H   A   N   I   S   T   A   N 377 377 377 377  \0 003
993 0000060  \0  \0  \0 002   A   L  \0  \0  \0 007   A   L   B   A   N   I
994 0000100   A 377 377 377 377  \0 003  \0  \0  \0 002   D   Z  \0  \0  \0
995 0000120 007   A   L   G   E   R   I   A 377 377 377 377  \0 003  \0  \0
996 0000140  \0 002   Z   M  \0  \0  \0 006   Z   A   M   B   I   A 377 377
997 0000160 377 377  \0 003  \0  \0  \0 002   Z   W  \0  \0  \0  \b   Z   I
998 0000200   M   B   A   B   W   E 377 377 377 377 377 377
999 </programlisting></para>
1000  </refsect1>
1001
1002  <refsect1>
1003   <title>Compatibility</title>
1004
1005   <para>
1006    There is no <command>COPY</command> statement in the SQL standard.
1007   </para>
1008
1009   <para>
1010    The following syntax was used before <productname>PostgreSQL</productname>
1011    version 9.0 and is still supported:
1012
1013 <synopsis>
1014 COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
1015     FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
1016     [ [ WITH ]
1017           [ BINARY ]
1018           [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter_character</replaceable>' ]
1019           [ NULL [ AS ] '<replaceable class="parameter">null_string</replaceable>' ]
1020           [ CSV [ HEADER ]
1021                 [ QUOTE [ AS ] '<replaceable class="parameter">quote_character</replaceable>' ]
1022                 [ ESCAPE [ AS ] '<replaceable class="parameter">escape_character</replaceable>' ]
1023                 [ FORCE NOT NULL <replaceable class="parameter">column_name</replaceable> [, ...] ] ] ]
1024
1025 COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
1026     TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
1027     [ [ WITH ]
1028           [ BINARY ]
1029           [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter_character</replaceable>' ]
1030           [ NULL [ AS ] '<replaceable class="parameter">null_string</replaceable>' ]
1031           [ CSV [ HEADER ]
1032                 [ QUOTE [ AS ] '<replaceable class="parameter">quote_character</replaceable>' ]
1033                 [ ESCAPE [ AS ] '<replaceable class="parameter">escape_character</replaceable>' ]
1034                 [ FORCE QUOTE { <replaceable class="parameter">column_name</replaceable> [, ...] | * } ] ] ]
1035 </synopsis>
1036
1037    Note that in this syntax, <literal>BINARY</literal> and <literal>CSV</literal> are
1038    treated as independent keywords, not as arguments of a <literal>FORMAT</literal>
1039    option.
1040   </para>
1041
1042   <para>
1043    The following syntax was used before <productname>PostgreSQL</productname>
1044    version 7.3 and is still supported:
1045
1046 <synopsis>
1047 COPY [ BINARY ] <replaceable class="parameter">table_name</replaceable>
1048     FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
1049     [ [USING] DELIMITERS '<replaceable class="parameter">delimiter_character</replaceable>' ]
1050     [ WITH NULL AS '<replaceable class="parameter">null_string</replaceable>' ]
1051
1052 COPY [ BINARY ] <replaceable class="parameter">table_name</replaceable>
1053     TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
1054     [ [USING] DELIMITERS '<replaceable class="parameter">delimiter_character</replaceable>' ]
1055     [ WITH NULL AS '<replaceable class="parameter">null_string</replaceable>' ]
1056 </synopsis></para>
1057  </refsect1>
1058 </refentry>