Imported upstream version 1.5
[manpages-zh.git] / raw / man7 / copy.7
blob68ed6dab97b9b4f9905d9777532f0c96e372ab05
1 .\\" auto-generated by docbook2man-spec $Revision: 1.1 $
2 .TH "COPY" "7" "2003-11-02" "SQL - Language Statements" "SQL Commands"
3 .SH NAME
4 COPY \- copy data between a file and a table
6 .SH SYNOPSIS
7 .sp
8 .nf
9 COPY \fItablename\fR [ ( \fIcolumn\fR [, ...] ) ]
10     FROM { '\fIfilename\fR' | STDIN }
11     [ [ WITH ] 
12           [ BINARY ] 
13           [ OIDS ]
14           [ DELIMITER [ AS ] '\fIdelimiter\fR' ]
15           [ NULL [ AS ] '\fInull string\fR' ] ]
17 COPY \fItablename\fR [ ( \fIcolumn\fR [, ...] ) ]
18     TO { '\fIfilename\fR' | STDOUT }
19     [ [ WITH ] 
20           [ BINARY ]
21           [ OIDS ]
22           [ DELIMITER [ AS ] '\fIdelimiter\fR' ]
23           [ NULL [ AS ] '\fInull string\fR' ] ]
24 .sp
25 .fi
26 .SH "DESCRIPTION"
27 .PP
28 \fBCOPY\fR moves data between
29 PostgreSQL tables and standard file-system
30 files. \fBCOPY TO\fR copies the contents of a table
31 \fBto\fR a file, while \fBCOPY FROM\fR copies
32 data \fBfrom\fR a file to a table (appending the data to
33 whatever is in the table already).
34 .PP
35 If a list of columns is specified, \fBCOPY\fR will
36 only copy the data in the specified columns to or from the file.
37 If there are any columns in the table that are not in the column list,
38 \fBCOPY FROM\fR will insert the default values for
39 those columns.
40 .PP
41 \fBCOPY\fR with a file name instructs the
42 PostgreSQL server to directly read from
43 or write to a file. The file must be accessible to the server and
44 the name must be specified from the viewpoint of the server. When
45 STDIN or STDOUT is
46 specified, data is transmitted via the connection between the
47 client and the server.
48 .SH "PARAMETERS"
49 .TP
50 \fB\fItablename\fB\fR
51 The name (optionally schema-qualified) of an existing table.
52 .TP
53 \fB\fIcolumn\fB\fR
54 An optional list of columns to be copied. If no column list is
55 specified, all columns will be used.
56 .TP
57 \fB\fIfilename\fB\fR
58 The absolute path name of the input or output file.
59 .TP
60 \fBSTDIN\fR
61 Specifies that input comes from the client application.
62 .TP
63 \fBSTDOUT\fR
64 Specifies that output goes to the client application.
65 .TP
66 \fBBINARY\fR
67 Causes all data to be stored or read in binary format rather
68 than as text. You cannot specify the \fBDELIMITER\fR
69 or \fBNULL\fR options in binary mode.
70 .TP
71 \fBOIDS\fR
72 Specifies copying the OID for each row. (An error is raised if
73 OIDS is specified for a table that does not
74 have OIDs.)
75 .TP
76 \fB\fIdelimiter\fB\fR
77 The single character that separates columns within each row
78 (line) of the file. The default is a tab character.
79 .TP
80 \fB\fInull string\fB\fR
81 The string that represents a null value. The default is
82 \\N (backslash-N). You might prefer an empty
83 string, for example.
84 .sp
85 .RS
86 .B "Note:"
87 On a \fBCOPY FROM\fR, any data item that matches
88 this string will be stored as a null value, so you should make
89 sure that you use the same string as you used with
90 \fBCOPY TO\fR.
91 .RE
92 .sp
93 .SH "NOTES"
94 .PP
95 \fBCOPY\fR can only be used with plain tables, not
96 with views.
97 .PP
98 The BINARY key word causes all data to be
99 stored/read as binary format rather than as text. It is
100 somewhat faster than the normal text mode, but a binary-format
101 file is less portable across machine architectures and
102 PostgreSQL versions.
104 You must have select privilege on the table
105 whose values are read by \fBCOPY TO\fR, and
106 insert privilege on the table into which values
107 are inserted by \fBCOPY FROM\fR.
109 Files named in a \fBCOPY\fR command are read or written
110 directly by the server, not by the client application. Therefore,
111 they must reside on or be accessible to the database server machine,
112 not the client. They must be accessible to and readable or writable
113 by the PostgreSQL user (the user ID the
114 server runs as), not the client. \fBCOPY\fR naming a
115 file is only allowed to database superusers, since it allows reading
116 or writing any file that the server has privileges to access.
118 Do not confuse \fBCOPY\fR with the
119 \fBpsql\fR instruction
120 \fB\\copy\fR. \fB\\copy\fR invokes
121 \fBCOPY FROM STDIN\fR or \fBCOPY TO
122 STDOUT\fR, and then fetches/stores the data in a file
123 accessible to the \fBpsql\fR client. Thus,
124 file accessibility and access rights depend on the client rather
125 than the server when \fB\\copy\fR is used.
127 It is recommended that the file name used in \fBCOPY\fR
128 always be specified as an absolute path. This is enforced by the
129 server in the case of \fBCOPY TO\fR, but for
130 \fBCOPY FROM\fR you do have the option of reading from
131 a file specified by a relative path. The path will be interpreted
132 relative to the working directory of the server process (somewhere below
133 the data directory), not the client's working directory.
135 \fBCOPY FROM\fR will invoke any triggers and check
136 constraints on the destination table. However, it will not invoke rules.
138 \fBCOPY\fR stops operation at the first error. This
139 should not lead to problems in the event of a \fBCOPY
140 TO\fR, but the target table will already have received
141 earlier rows in a \fBCOPY FROM\fR. These rows will not
142 be visible or accessible, but they still occupy disk space. This may
143 amount to a considerable amount of wasted disk space if the failure
144 happened well into a large copy operation. You may wish to invoke
145 \fBVACUUM\fR to recover the wasted space.
146 .SH "FILE FORMATS"
147 .SS "TEXT FORMAT"
149 When \fBCOPY\fR is used without the BINARY option,
150 the data read or written is a text file with one line per table row.
151 Columns in a row are separated by the delimiter character.
152 The column values themselves are strings generated by the
153 output function, or acceptable to the input function, of each
154 attribute's data type. The specified null string is used in
155 place of columns that are null.
156 \fBCOPY FROM\fR will raise an error if any line of the
157 input file contains more or fewer columns than are expected.
158 If OIDS is specified, the OID is read or written as the first column,
159 preceding the user data columns.
161 End of data can be represented by a single line containing just
162 backslash-period (\\.). An end-of-data marker is
163 not necessary when reading from a file, since the end of file
164 serves perfectly well; it is needed only when copying data to or from
165 client applications using pre-3.0 client protocol.
167 Backslash characters (\\) may be used in the
168 \fBCOPY\fR data to quote data characters that might
169 otherwise be taken as row or column delimiters. In particular, the
170 following characters \fBmust\fR be preceded by a backslash if
171 they appear as part of a column value: backslash itself,
172 newline, carriage return, and the current delimiter character.
174 The specified null string is sent by \fBCOPY TO\fR without
175 adding any backslashes; conversely, \fBCOPY FROM\fR matches
176 the input against the null string before removing backslashes. Therefore,
177 a null string such as \\N cannot be confused with
178 the actual data value \\N (which would be represented
179 as \\\\N).
181 The following special backslash sequences are recognized by
182 \fBCOPY FROM\fR:
183 SequenceRepresents\\bBackspace (ASCII 8)\\fForm feed (ASCII 12)\\nNewline (ASCII 10)\\rCarriage return (ASCII 13)\\tTab (ASCII 9)\\vVertical tab (ASCII 11)\\\fIdigits\fRBackslash followed by one to three octal digits specifies
184 the character with that numeric code
185 Presently, \fBCOPY TO\fR will never emit an octal-digits
186 backslash sequence, but it does use the other sequences listed above
187 for those control characters.
189 Any other backslashed character that is not mentioned in the above table
190 will be taken to represent itself. However, beware of adding backslashes
191 unnecessarily, since that might accidentally produce a string matching the
192 end-of-data marker (\\.) or the null string (\\N by
193 default). These strings will be recognized before any other backslash
194 processing is done.
196 It is strongly recommended that applications generating COPY data convert
197 data newlines and carriage returns to the \\n and
198 \\r sequences respectively. At present it is
199 possible to represent a data carriage return by a backslash and carriage
200 return, and to represent a data newline by a backslash and newline. 
201 However, these representations might not be accepted in future releases.
202 They are also highly vulnerable to corruption if the COPY file is
203 transferred across different machines (for example, from Unix to Windows
204 or vice versa).
206 \fBCOPY TO\fR will terminate each row with a Unix-style 
207 newline (``\\n''). Servers running on MS Windows instead
208 output carriage return/newline (``\\r\\n''), but only for
209 \fBCOPY\fR to a server file; for consistency across platforms,
210 \fBCOPY TO STDOUT\fR always sends ``\\n''
211 regardless of server platform.
212 \fBCOPY FROM\fR can handle lines ending with newlines,
213 carriage returns, or carriage return/newlines. To reduce the risk of
214 error due to un-backslashed newlines or carriage returns that were
215 meant as data, \fBCOPY FROM\fR will complain if the line
216 endings in the input are not all alike.
217 .SS "BINARY FORMAT"
219 The file format used for \fBCOPY BINARY\fR changed in
220 PostgreSQL 7.4. The new format consists
221 of a file header, zero or more tuples containing the row data, and
222 a file trailer. Headers and data are now in network byte order.
223 .SS "FILE HEADER"
225 The file header consists of 15 bytes of fixed fields, followed
226 by a variable-length header extension area. The fixed fields are:
228 \fBSignature\fR
229 11-byte sequence PGCOPY\\n\\377\\r\\n\\0 --- note that the zero byte
230 is a required part of the signature. (The signature is designed to allow
231 easy identification of files that have been munged by a non-8-bit-clean
232 transfer. This signature will be changed by end-of-line-translation
233 filters, dropped zero bytes, dropped high bits, or parity changes.)
235 \fBFlags field\fR
236 32-bit integer bit mask to denote important aspects of the file format. Bits
237 are numbered from 0 (LSB) to 31 (MSB). Note that
238 this field is stored in network byte order (most significant byte first),
239 as are all the integer fields used in the file format. Bits
240 16-31 are reserved to denote critical file format issues; a reader
241 should abort if it finds an unexpected bit set in this range. Bits 0-15
242 are reserved to signal backwards-compatible format issues; a reader
243 should simply ignore any unexpected bits set in this range. Currently
244 only one flag bit is defined, and the rest must be zero:
247 \fBBit 16\fR
248 if 1, OIDs are included in the data; if 0, not
252 \fBHeader extension area length\fR
253 32-bit integer, length in bytes of remainder of header, not including self.
254 Currently, this is zero, and the first tuple follows
255 immediately. Future changes to the format might allow additional data
256 to be present in the header. A reader should silently skip over any header
257 extension data it does not know what to do with.
260 The header extension area is envisioned to contain a sequence of
261 self-identifying chunks. The flags field is not intended to tell readers
262 what is in the extension area. Specific design of header extension contents
263 is left for a later release.
265 This design allows for both backwards-compatible header additions (add
266 header extension chunks, or set low-order flag bits) and
267 non-backwards-compatible changes (set high-order flag bits to signal such
268 changes, and add supporting data to the extension area if needed).
269 .SS "TUPLES"
271 Each tuple begins with a 16-bit integer count of the number of fields in the
272 tuple. (Presently, all tuples in a table will have the same count, but that
273 might not always be true.) Then, repeated for each field in the tuple, there
274 is a 32-bit length word followed by that many bytes of field data. (The
275 length word does not include itself, and can be zero.) As a special case,
276 -1 indicates a NULL field value. No value bytes follow in the NULL case.
278 There is no alignment padding or any other extra data between fields.
280 Presently, all data values in a \fBCOPY BINARY\fR file are
281 assumed to be in binary format (format code one). It is anticipated that a
282 future extension may add a header field that allows per-column format codes
283 to be specified.
285 To determine the appropriate binary format for the actual tuple data you
286 should consult the PostgreSQL source, in
287 particular the \fB*send\fR and \fB*recv\fR functions for
288 each column's data type (typically these functions are found in the
289 \fIsrc/backend/utils/adt/\fR directory of the source
290 distribution).
292 If OIDs are included in the file, the OID field immediately follows the
293 field-count word. It is a normal field except that it's not included
294 in the field-count. In particular it has a length word --- this will allow
295 handling of 4-byte vs. 8-byte OIDs without too much pain, and will allow
296 OIDs to be shown as null if that ever proves desirable.
297 .SS "FILE TRAILER"
299 The file trailer consists of a 16-bit integer word containing -1. This
300 is easily distinguished from a tuple's field-count word.
302 A reader should report an error if a field-count word is neither -1
303 nor the expected number of columns. This provides an extra
304 check against somehow getting out of sync with the data.
305 .SH "EXAMPLES"
307 The following example copies a table to the client
308 using the vertical bar (|) as the field delimiter:
311 COPY country TO STDOUT WITH DELIMITER '|';
315 To copy data from a file into the country table:
318 COPY country FROM '/usr1/proj/bray/sql/country_data';
322 Here is a sample of data suitable for copying into a table from
323 STDIN:
326 AF      AFGHANISTAN
327 AL      ALBANIA
328 DZ      ALGERIA
329 ZM      ZAMBIA
330 ZW      ZIMBABWE
333 Note that the white space on each line is actually a tab character.
335 The following is the same data, output in binary format.
336 The data is shown after filtering through the
337 Unix utility \fBod -c\fR. The table has three columns;
338 the first has type \fBchar(2)\fR, the second has type \fBtext\fR,
339 and the third has type \fBinteger\fR. All the rows have a null value
340 in the third column.
343 0000000   P   G   C   O   P   Y  \\n 377  \\r  \\n  \\0  \\0  \\0  \\0  \\0  \\0
344 0000020  \\0  \\0  \\0  \\0 003  \\0  \\0  \\0 002   A   F  \\0  \\0  \\0 013   A
345 0000040   F   G   H   A   N   I   S   T   A   N 377 377 377 377  \\0 003
346 0000060  \\0  \\0  \\0 002   A   L  \\0  \\0  \\0 007   A   L   B   A   N   I
347 0000100   A 377 377 377 377  \\0 003  \\0  \\0  \\0 002   D   Z  \\0  \\0  \\0
348 0000120 007   A   L   G   E   R   I   A 377 377 377 377  \\0 003  \\0  \\0
349 0000140  \\0 002   Z   M  \\0  \\0  \\0 006   Z   A   M   B   I   A 377 377
350 0000160 377 377  \\0 003  \\0  \\0  \\0 002   Z   W  \\0  \\0  \\0  \\b   Z   I
351 0000200   M   B   A   B   W   E 377 377 377 377 377 377
354 .SH "COMPATIBILITY"
356 There is no \fBCOPY\fR statement in the SQL standard.
358 The following syntax was used before PostgreSQL version 7.3 and is
359 still supported:
362 COPY [ BINARY ] \fItablename\fR [ WITH OIDS ]
363     FROM { '\fIfilename\fR' | STDIN }
364     [ [USING] DELIMITERS '\fIdelimiter\fR' ]
365     [ WITH NULL AS '\fInull string\fR' ]
367 COPY [ BINARY ] \fItablename\fR [ WITH OIDS ]
368     TO { '\fIfilename\fR' | STDOUT }
369     [ [USING] DELIMITERS '\fIdelimiter\fR' ]
370     [ WITH NULL AS '\fInull string\fR' ]