1
1
<!--
2
- $PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.91 2009/09/19 10:23:26 petere Exp $
2
+ $PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.92 2009/09/21 20: 10:21 tgl Exp $
3
3
PostgreSQL documentation
4
4
-->
5
5
@@ -24,27 +24,23 @@ PostgreSQL documentation
24
24
<synopsis>
25
25
COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
26
26
FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
27
- [ [ WITH ]
28
- [ BINARY ]
29
- [ OIDS ]
30
- [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
31
- [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
32
- [ CSV [ HEADER ]
33
- [ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
34
- [ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
35
- [ FORCE NOT NULL <replaceable class="parameter">column</replaceable> [, ...] ]
27
+ [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
36
28
37
29
COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
38
30
TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
39
- [ [ WITH ]
40
- [ BINARY ]
41
- [ OIDS ]
42
- [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
43
- [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
44
- [ CSV [ HEADER ]
45
- [ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
46
- [ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
47
- [ FORCE QUOTE { <replaceable class="parameter">column</replaceable> [, ...] | * } ]
31
+ [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
32
+
33
+ <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
34
+
35
+ FORMAT <replaceable class="parameter">format_name</replaceable>
36
+ OIDS [ <replaceable class="parameter">boolean</replaceable> ]
37
+ DELIMITER '<replaceable class="parameter">delimiter_character</replaceable>'
38
+ NULL '<replaceable class="parameter">null_string</replaceable>'
39
+ HEADER [ <replaceable class="parameter">boolean</replaceable> ]
40
+ QUOTE '<replaceable class="parameter">quote_character</replaceable>'
41
+ ESCAPE '<replaceable class="parameter">escape_character</replaceable>'
42
+ FORCE_QUOTE { ( <replaceable class="parameter">column</replaceable> [, ...] ) | * }
43
+ FORCE_NOT_NULL ( <replaceable class="parameter">column</replaceable> [, ...] )
48
44
</synopsis>
49
45
</refsynopsisdiv>
50
46
@@ -120,8 +116,8 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
120
116
<listitem>
121
117
<para>
122
118
The absolute path name of the input or output file. Windows users
123
- might need to use an <literal>E''</> string and double backslashes
124
- used as path separators .
119
+ might need to use an <literal>E''</> string and double any backslashes
120
+ used in the path name .
125
121
</para>
126
122
</listitem>
127
123
</varlistentry>
@@ -145,12 +141,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
145
141
</varlistentry>
146
142
147
143
<varlistentry>
148
- <term><literal>BINARY</literal ></term>
144
+ <term><replaceable class="parameter">boolean</replaceable ></term>
149
145
<listitem>
150
146
<para>
151
- Causes all data to be stored or read in binary format rather
152
- than as text. You cannot specify the <option>DELIMITER</option>,
153
- <option>NULL</option>, or <option>CSV</> options in binary mode.
147
+ Specifies whether the selected option should be turned on or off.
148
+ You can write <literal>TRUE</literal>, <literal>ON</>, or
149
+ <literal>1</literal> to enable the option, and <literal>FALSE</literal>,
150
+ <literal>OFF</>, or <literal>0</literal> to disable it. The
151
+ <replaceable class="parameter">boolean</replaceable> value can also
152
+ be omitted, in which case <literal>TRUE</literal> is assumed.
153
+ </para>
154
+ </listitem>
155
+ </varlistentry>
156
+
157
+ <varlistentry>
158
+ <term><literal>FORMAT</literal></term>
159
+ <listitem>
160
+ <para>
161
+ Selects the data format to be read or written:
162
+ <literal>text</>,
163
+ <literal>csv</> (Comma Separated Values),
164
+ or <literal>binary</>.
165
+ The default is <literal>text</>.
154
166
</para>
155
167
</listitem>
156
168
</varlistentry>
@@ -168,25 +180,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
168
180
</varlistentry>
169
181
170
182
<varlistentry>
171
- <term><replaceable class="parameter">delimiter</replaceable ></term>
183
+ <term><literal>DELIMITER</literal ></term>
172
184
<listitem>
173
185
<para>
174
- The single ASCII character that separates columns within each row
175
- (line) of the file. The default is a tab character in text mode,
176
- a comma in <literal>CSV</> mode.
186
+ Specifies the character that separates columns within each row
187
+ (line) of the file. The default is a tab character in text format,
188
+ a comma in <literal>CSV</> format.
189
+ This must be a single one-byte character.
190
+ This option is not allowed when using <literal>binary</> format.
177
191
</para>
178
192
</listitem>
179
193
</varlistentry>
180
194
181
195
<varlistentry>
182
- <term><replaceable class="parameter">null string</replaceable ></term>
196
+ <term><literal>NULL</literal ></term>
183
197
<listitem>
184
198
<para>
185
- The string that represents a null value. The default is
186
- <literal>\N</literal> (backslash-N) in text mode , and an unquoted empty
187
- string in <literal>CSV</> mode . You might prefer an
188
- empty string even in text mode for cases where you don't want to
199
+ Specifies the string that represents a null value. The default is
200
+ <literal>\N</literal> (backslash-N) in text format , and an unquoted empty
201
+ string in <literal>CSV</> format . You might prefer an
202
+ empty string even in text format for cases where you don't want to
189
203
distinguish nulls from empty strings.
204
+ This option is not allowed when using <literal>binary</> format.
190
205
</para>
191
206
192
207
<note>
@@ -201,68 +216,68 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
201
216
</listitem>
202
217
</varlistentry>
203
218
204
- <varlistentry>
205
- <term><literal>CSV</literal></term>
206
- <listitem>
207
- <para>
208
- Selects Comma Separated Value (<literal>CSV</>) mode.
209
- </para>
210
- </listitem>
211
- </varlistentry>
212
-
213
219
<varlistentry>
214
220
<term><literal>HEADER</literal></term>
215
221
<listitem>
216
222
<para>
217
223
Specifies that the file contains a header line with the names of each
218
224
column in the file. On output, the first line contains the column
219
225
names from the table, and on input, the first line is ignored.
226
+ This option is allowed only when using <literal>CSV</> format.
220
227
</para>
221
228
</listitem>
222
229
</varlistentry>
223
230
224
231
<varlistentry>
225
- <term><replaceable class="parameter">quote</replaceable ></term>
232
+ <term><literal>QUOTE</literal ></term>
226
233
<listitem>
227
234
<para>
228
- Specifies the ASCII quotation character in <literal>CSV</> mode .
235
+ Specifies the quoting character to be used when a data value is quoted .
229
236
The default is double-quote.
237
+ This must be a single one-byte character.
238
+ This option is allowed only when using <literal>CSV</> format.
230
239
</para>
231
240
</listitem>
232
241
</varlistentry>
233
242
234
243
<varlistentry>
235
- <term><replaceable class="parameter">escape</replaceable ></term>
244
+ <term><literal>ESCAPE</literal ></term>
236
245
<listitem>
237
246
<para>
238
- Specifies the ASCII character that should appear before a
239
- <literal>QUOTE</> data character value in <literal>CSV</> mode.
240
- The default is the <literal>QUOTE</> value (usually double-quote).
247
+ Specifies the character that should appear before a
248
+ data character that matches the <literal>QUOTE</> value.
249
+ The default is the same as the <literal>QUOTE</> value (so that
250
+ the quoting character is doubled if it appears in the data).
251
+ This must be a single one-byte character.
252
+ This option is allowed only when using <literal>CSV</> format.
241
253
</para>
242
254
</listitem>
243
255
</varlistentry>
244
256
245
257
<varlistentry>
246
- <term><literal>FORCE QUOTE </></term>
258
+ <term><literal>FORCE_QUOTE </></term>
247
259
<listitem>
248
260
<para>
249
- In <literal>CSV</> <command>COPY TO</> mode, forces quoting to be
261
+ Forces quoting to be
250
262
used for all non-<literal>NULL</> values in each specified column.
251
263
<literal>NULL</> output is never quoted. If <literal>*</> is specified,
252
264
non-<literal>NULL</> values will be quoted in all columns.
265
+ This option is allowed only in <command>COPY TO</>, and only when
266
+ using <literal>CSV</> format.
253
267
</para>
254
268
</listitem>
255
269
</varlistentry>
256
270
257
271
<varlistentry>
258
- <term><literal>FORCE NOT NULL </></term>
272
+ <term><literal>FORCE_NOT_NULL </></term>
259
273
<listitem>
260
274
<para>
261
- In <literal>CSV</> <command>COPY FROM</> mode, process each
262
- specified column as though it were quoted and hence not a
263
- <literal>NULL</> value. For the default null string in
264
- <literal>CSV</> mode (<literal>''</>), this causes missing
265
- values to be input as zero-length strings.
275
+ Do not match the specified columns' values against the null string.
276
+ In the default case where the null string is empty, this means that
277
+ empty values will be read as zero-length strings rather than nulls,
278
+ even when they are not quoted.
279
+ This option is allowed only in <command>COPY FROM</>, and only when
280
+ using <literal>CSV</> format.
266
281
</para>
267
282
</listitem>
268
283
</varlistentry>
@@ -293,18 +308,6 @@ COPY <replaceable class="parameter">count</replaceable>
293
308
<replaceable class="parameter">viewname</replaceable>) TO ...</literal>.
294
309
</para>
295
310
296
- <para>
297
- The <literal>BINARY</literal> key word causes all data to be
298
- stored/read as binary format rather than as text. It is
299
- somewhat faster than the normal text mode, but a binary-format
300
- file is less portable across machine architectures and
301
- <productname>PostgreSQL</productname> versions.
302
- Also, the binary format is very data type specific; for example
303
- it will not work to output binary data from a <type>smallint</> column
304
- and read it into an <type>integer</> column, even though that would work
305
- fine in text format.
306
- </para>
307
-
308
311
<para>
309
312
You must have select privilege on the table
310
313
whose values are read by <command>COPY TO</command>, and
@@ -390,8 +393,7 @@ COPY <replaceable class="parameter">count</replaceable>
390
393
<title>Text Format</title>
391
394
392
395
<para>
393
- When <command>COPY</command> is used without the <literal>BINARY</literal>
394
- or <literal>CSV</> options,
396
+ When the <literal>text</> format is used,
395
397
the data read or written is a text file with one line per table row.
396
398
Columns in a row are separated by the delimiter character.
397
399
The column values themselves are strings generated by the
@@ -527,10 +529,10 @@ COPY <replaceable class="parameter">count</replaceable>
527
529
<title>CSV Format</title>
528
530
529
531
<para>
530
- This format is used for importing and exporting the Comma
532
+ This format option is used for importing and exporting the Comma
531
533
Separated Value (<literal>CSV</>) file format used by many other
532
- programs, such as spreadsheets. Instead of the escaping used by
533
- <productname>PostgreSQL</productname>'s standard text mode , it
534
+ programs, such as spreadsheets. Instead of the escaping rules used by
535
+ <productname>PostgreSQL</productname>'s standard text format , it
534
536
produces and recognizes the common CSV escaping mechanism.
535
537
</para>
536
538
@@ -542,7 +544,7 @@ COPY <replaceable class="parameter">count</replaceable>
542
544
suffixed by the <literal>QUOTE</> character, and any occurrence
543
545
within the value of a <literal>QUOTE</> character or the
544
546
<literal>ESCAPE</> character is preceded by the escape character.
545
- You can also use <literal>FORCE QUOTE </> to force quotes when outputting
547
+ You can also use <literal>FORCE_QUOTE </> to force quotes when outputting
546
548
non-<literal>NULL</> values in specific columns.
547
549
</para>
548
550
@@ -556,7 +558,7 @@ COPY <replaceable class="parameter">count</replaceable>
556
558
default settings, a <literal>NULL</> is written as an unquoted empty
557
559
string, while an empty string data value is written with double quotes
558
560
(<literal>""</>). Reading values follows similar rules. You can
559
- use <literal>FORCE NOT NULL </> to prevent <literal>NULL</> input
561
+ use <literal>FORCE_NOT_NULL </> to prevent <literal>NULL</> input
560
562
comparisons for specific columns.
561
563
</para>
562
564
@@ -574,7 +576,7 @@ COPY <replaceable class="parameter">count</replaceable>
574
576
575
577
<note>
576
578
<para>
577
- In <literal>CSV</> mode , all characters are significant. A quoted value
579
+ In <literal>CSV</> format , all characters are significant. A quoted value
578
580
surrounded by white space, or any characters other than
579
581
<literal>DELIMITER</>, will include those characters. This can cause
580
582
errors if you import data from a system that pads <literal>CSV</>
@@ -587,9 +589,9 @@ COPY <replaceable class="parameter">count</replaceable>
587
589
588
590
<note>
589
591
<para>
590
- CSV mode will both recognize and produce CSV files with quoted
592
+ CSV format will both recognize and produce CSV files with quoted
591
593
values containing embedded carriage returns and line feeds. Thus
592
- the files are not strictly one line per table row like text-mode
594
+ the files are not strictly one line per table row like text-format
593
595
files.
594
596
</para>
595
597
</note>
@@ -610,12 +612,30 @@ COPY <replaceable class="parameter">count</replaceable>
610
612
<title>Binary Format</title>
611
613
612
614
<para>
613
- The file format used for <command>COPY BINARY</command> changed in
614
- <productname>PostgreSQL</productname> 7.4. The new format consists
615
+ The <literal>binary</literal> format option causes all data to be
616
+ stored/read as binary format rather than as text. It is
617
+ somewhat faster than the text and <literal>CSV</> formats,
618
+ but a binary-format file is less portable across machine architectures and
619
+ <productname>PostgreSQL</productname> versions.
620
+ Also, the binary format is very data type specific; for example
621
+ it will not work to output binary data from a <type>smallint</> column
622
+ and read it into an <type>integer</> column, even though that would work
623
+ fine in text format.
624
+ </para>
625
+
626
+ <para>
627
+ The <literal>binary</> file format consists
615
628
of a file header, zero or more tuples containing the row data, and
616
- a file trailer. Headers and data are now in network byte order.
629
+ a file trailer. Headers and data are in network byte order.
617
630
</para>
618
631
632
+ <note>
633
+ <para>
634
+ <productname>PostgreSQL</productname> releases before 7.4 used a
635
+ different binary file format.
636
+ </para>
637
+ </note>
638
+
619
639
<refsect3>
620
640
<title>File Header</title>
621
641
@@ -710,7 +730,7 @@ There is no alignment padding or any other extra data between fields.
710
730
</para>
711
731
712
732
<para>
713
- Presently, all data values in a <command>COPY BINARY</command> file are
733
+ Presently, all data values in a binary-format file are
714
734
assumed to be in binary format (format code one). It is anticipated that a
715
735
future extension might add a header field that allows per-column format codes
716
736
to be specified.
@@ -758,7 +778,7 @@ OIDs to be shown as null if that ever proves desirable.
758
778
The following example copies a table to the client
759
779
using the vertical bar (<literal>|</literal>) as the field delimiter:
760
780
<programlisting>
761
- COPY country TO STDOUT WITH DELIMITER '|';
781
+ COPY country TO STDOUT ( DELIMITER '|') ;
762
782
</programlisting>
763
783
</para>
764
784
@@ -817,6 +837,41 @@ ZW ZIMBABWE
817
837
There is no <command>COPY</command> statement in the SQL standard.
818
838
</para>
819
839
840
+ <para>
841
+ The following syntax was used before <productname>PostgreSQL</>
842
+ version 8.5 and is still supported:
843
+
844
+ <synopsis>
845
+ COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
846
+ FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
847
+ [ [ WITH ]
848
+ [ BINARY ]
849
+ [ OIDS ]
850
+ [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
851
+ [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
852
+ [ CSV [ HEADER ]
853
+ [ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
854
+ [ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
855
+ [ FORCE NOT NULL <replaceable class="parameter">column</replaceable> [, ...] ]
856
+
857
+ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
858
+ TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
859
+ [ [ WITH ]
860
+ [ BINARY ]
861
+ [ OIDS ]
862
+ [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
863
+ [ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
864
+ [ CSV [ HEADER ]
865
+ [ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
866
+ [ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
867
+ [ FORCE QUOTE { <replaceable class="parameter">column</replaceable> [, ...] | * } ]
868
+ </synopsis>
869
+
870
+ Note that in this syntax, <literal>BINARY</> and <literal>CSV</> are
871
+ treated as independent keywords, not as arguments of a <literal>FORMAT</>
872
+ option.
873
+ </para>
874
+
820
875
<para>
821
876
The following syntax was used before <productname>PostgreSQL</>
822
877
version 7.3 and is still supported:
0 commit comments