11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.55 2003/12/13 23:59:07 neilc Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.56 2004/04/19 17:22:30 momjian Exp $
33PostgreSQL documentation
44-->
55
@@ -26,15 +26,21 @@ COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable cla
2626 [ BINARY ]
2727 [ OIDS ]
2828 [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
29- [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ] ]
29+ [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
30+ [ CSV [ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
31+ [ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
32+ [ LITERAL <replaceable class="parameter">column</replaceable> [, ...] ]
3033
3134COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
3235 TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
3336 [ [ WITH ]
3437 [ BINARY ]
3538 [ OIDS ]
3639 [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
37- [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ] ]
40+ [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
41+ [ CSV [ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
42+ [ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
43+ [ FORCE <replaceable class="parameter">column</replaceable> [, ...] ]
3844</synopsis>
3945 </refsynopsisdiv>
4046
@@ -146,7 +152,8 @@ COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable cla
146152 <listitem>
147153 <para>
148154 The single character that separates columns within each row
149- (line) of the file. The default is a tab character.
155+ (line) of the file. The default is a tab character in text mode,
156+ a comma in <literal>CSV</> mode.
150157 </para>
151158 </listitem>
152159 </varlistentry>
@@ -156,20 +163,86 @@ COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable cla
156163 <listitem>
157164 <para>
158165 The string that represents a null value. The default is
159- <literal>\N</literal> (backslash-N). You might prefer an empty
160- string, for example.
166+ <literal>\N</literal> (backslash-N) in text mode, and a empty
167+ value with no quotes in <literal>CSV</> mode. You might prefer an
168+ empty string even in text mode for cases where you don't want to
169+ distinguish nulls from empty strings.
161170 </para>
162171
163172 <note>
164173 <para>
165- On a <command>COPY FROM</command>, any data item that matches
174+ When using <command>COPY FROM</command>, any data item that matches
166175 this string will be stored as a null value, so you should make
167176 sure that you use the same string as you used with
168177 <command>COPY TO</command>.
169178 </para>
170179 </note>
180+
181+ </listitem>
182+ </varlistentry>
183+
184+ <varlistentry>
185+ <term><literal>CSV</literal></term>
186+ <listitem>
187+ <para>
188+ Enables Comma Separated Variable (<literal>CSV</>) mode. (Also called
189+ Comma Separated Value). It sets the default <literal>DELIMITER</> to
190+ comma, and <literal>QUOTE</> and <literal>ESCAPE</> values to
191+ double-quote.
192+ </para>
193+ </listitem>
194+ </varlistentry>
195+
196+ <varlistentry>
197+ <term><replaceable class="parameter">quote</replaceable></term>
198+ <listitem>
199+ <para>
200+ Specifies the quotation character in <literal>CSV</> mode.
201+ The default is double-quote.
202+ </para>
171203 </listitem>
172204 </varlistentry>
205+
206+ <varlistentry>
207+ <term><replaceable class="parameter">escape</replaceable></term>
208+ <listitem>
209+ <para>
210+ Specifies the character that should appear before a <literal>QUOTE</>
211+ data character value in <literal>CSV</> mode. The default is the
212+ <literal>QUOTE</> value (usually double-quote).
213+ </para>
214+ </listitem>
215+ </varlistentry>
216+
217+ <varlistentry>
218+ <term><literal>FORCE</></term>
219+ <listitem>
220+ <para>
221+ In <literal>CSV</> <command>COPY TO</> mode, forces quoting
222+ to be used for all non-<literal>NULL</> values in each specified
223+ column. <literal>NULL</> output is never quoted.
224+ </para>
225+ </listitem>
226+ </varlistentry>
227+
228+ <varlistentry>
229+ <term><literal>LITERAL</></term>
230+ <listitem>
231+ <para>
232+ In <literal>CSV</> <command>COPY FROM</> mode, for each column specified,
233+ do not do a <literal>null string</> comparison; instead load the value
234+ literally. <literal>QUOTE</> and <literal>ESCAPE</> processing are still
235+ performed.
236+ </para>
237+ <para>
238+ If the <literal>null string</> is <literal>''</> (the default
239+ in <literal>CSV</> mode), a missing input value (<literal>delimiter,
240+ delimiter</>), will load as a zero-length string. <literal>Delimiter, quote,
241+ quote, delimiter</> is always treated as a zero-length string on input.
242+ </para>
243+ </listitem>
244+ </varlistentry>
245+
173246 </variablelist>
174247 </refsect1>
175248
@@ -233,6 +306,17 @@ COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable cla
233306 constraints on the destination table. However, it will not invoke rules.
234307 </para>
235308
309+ <para>
310+ <command>COPY</command> input and output is affected by
311+ <varname>DateStyle </varname>. For portability with other
312+ <productname>PostgreSQL</productname> installations which might use
313+ non-default <varname>DateStyle</varname> settings,
314+ <varname>DateStyle</varname> should be set to <literal>ISO</> before
315+ using <command>COPY</>. In <literal>CSV</> mode, use <literal>ISO</>
316+ or a <varname>DateStyle</varname> setting appropriate for the
317+ external application.
318+ </para>
319+
236320 <para>
237321 <command>COPY</command> stops operation at the first error. This
238322 should not lead to problems in the event of a <command>COPY
@@ -253,7 +337,8 @@ COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable cla
253337
254338 <para>
255339 When <command>COPY</command> is used without the <literal>BINARY</literal> option,
256- the data read or written is a text file with one line per table row.
340+ the data read or written is a text file with one line per table row,
341+ unless <literal>CSV</> mode is used.
257342 Columns in a row are separated by the delimiter character.
258343 The column values themselves are strings generated by the
259344 output function, or acceptable to the input function, of each
@@ -379,6 +464,63 @@ COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable cla
379464 </para>
380465 </refsect2>
381466
467+ <refsect2>
468+ <title>CSV Format</title>
469+
470+ <para>
471+ This format is used for importing and exporting the Comma
472+ Separated Variable (<literal>CSV</>) file format used by many other
473+ programs, such as spreadsheets. Instead of the escaping used by
474+ <productname>PostgreSQL</productname>'s standard text mode, it
475+ produces and recognises the common CSV escaping mechanism.
476+ </para>
477+
478+ <para>
479+ The values in each record are separated by the <literal>DELIMITER</>
480+ character. If the value contains the delimiter character, the
481+ <literal>QUOTE</> character, the <literal>NULL</> string, a carriage
482+ return, or line feed character, then the whole value is prefixed and
483+ suffixed by the <literal>QUOTE</> character, and any occurrence
484+ within the value of a <literal>QUOTE</> character or the
485+ <literal>ESCAPE</> character is preceded by the escape character.
486+ You can also use <literal>FORCE</> to force quotes when outputting
487+ non-<literal>NULL</> values in specific columns.
488+ </para>
489+
490+ <para>
491+ In general, the <literal>CSV</> format has no way to distinguish a
492+ <literal>NULL</> from an empty string.
493+ <productname>PostgreSQL</productname>'s COPY handles this by
494+ quoting. A <literal>NULL</> is output as the <literal>NULL</> string
495+ and is not quoted, while a data value matching the <literal>NULL</> string
496+ is quoted. Therefore, using the default settings, a <literal>NULL</> is
497+ written as an unquoted empty string, while an empty string is
498+ written with double quotes (<literal>""</>). Reading values follows
499+ similar rules. You can use <literal>LITERAL</> to prevent <literal>NULL</>
500+ input comparisons for specific columns.
501+ </para>
502+
503+ <note>
504+ <para>
505+ CSV mode will both recognize and produce CSV files with quoted
506+ values containing embedded carriage returns and line feeds. Thus
507+ the files are not strictly one line per table row like text-mode
508+ files.
509+ </para>
510+ </note>
511+
512+ <note>
513+ <para>
514+ Many programs produce strange and occasionally perverse CSV files,
515+ so the file format is more a convention than a standard. Thus you
516+ might encounter some files that cannot be imported using this
517+ mechanism, and <command>COPY</> might produce files that other
518+ programs can not process.
519+ </para>
520+ </note>
521+
522+ </refsect2>
523+
382524 <refsect2>
383525 <title>Binary Format</title>
384526