1 <REFENTRY ID="SQL-COPY">
6 <REFMISCINFO>SQL - Language Statements</REFMISCINFO>
13 Copies data between files and tables
17 <DATE>1998-04-15</DATE>
20 COPY [BINARY] <replaceable class="parameter">table</replaceable> [WITH OIDS]
21 TO|FROM '<replaceable class="parameter">filename</replaceable>'|stdin|stdout
22 [USING DELIMITERS '<replaceable class="parameter">delimiter</replaceable>']
25 <REFSECT2 ID="R2-SQL-COPY-1">
27 <DATE>1998-04-15</DATE>
43 <ReturnValue><replaceable class="parameter">table</replaceable></ReturnValue>
53 <ReturnValue><replaceable class="parameter">delimiter</replaceable></ReturnValue>
57 A character that delimits fields.
67 <REFSECT2 ID="R2-SQL-COPY-2">
69 <DATE>1998-04-15</DATE>
86 <ReturnValue>COPY</ReturnValue>
90 The copy completed successfully.
96 <ReturnValue>ERROR: <replaceable>error message</replaceable></ReturnValue>
100 The copy failed for the reason stated in the error message.
111 <REFSECT1 ID="R1-SQL-COPY-1">
113 <DATE>1998-04-15</DATE>
119 <command>COPY</command> moves data between PostgreSQL tables and
120 standard Unix files. The keyword <function>BINARY</function>
121 changes the behavior of field formatting, as described
122 below. <replaceable class="parameter">Table</replaceable> is the
123 name of an existing table. The keyword <function>WITH
124 OIDS</function> copies the internal unique object id (OID) for each
125 row. <replaceable class="parameter">Filename</replaceable> is the
126 absolute Unix pathname of the file. In place of a filename, the
127 keywords <function>stdin</function> and <function>stdout</function>
128 can be used, so that input to <command>COPY</command> can be written
129 by a libpq application and output from <command>COPY</command> can
130 be read by a libpq application.
133 The <function>BINARY</function> keyword will force all data to be
134 stored/read as binary objects rather than as ASCII text. It is
135 somewhat faster than the normal copy command, but is not
136 generally portable, and the files generated are somewhat larger,
137 although this factor is highly dependent on the data itself. By
138 default, an ASCII copy uses a tab (\t) character as a delimiter.
139 The delimiter may also be changed to any other single character
140 with the keyword <function>USING DELIMITERS</function>. Characters
141 in data fields which happen to match the delimiter character will
145 <REFSECT2 ID="R2-SQL-COPY-3">
147 <DATE>1998-04-15</DATE>
153 You must have select access on any table whose values are read by
154 <command>COPY</command>, and either insert or update access to a
155 table into which values are being inserted by <command>COPY</command>.
156 The backend also needs appropriate Unix permissions for any file read
157 or written by <command>COPY</command>.
159 Is this right? The man page talked of read, write and append access, which
160 is neither SQL nor Unix terminology.
164 The keyword <function>USING DELIMITERS</function> is inaptly
165 named, since only a single character may be specified. (If a
166 group of characters is specified, only the first character is
170 WARNING: do not confuse <command>COPY</command> with the
171 <command>psql</command> instruction <command>\copy</command>.
175 <refsect1 ID="R1-SQL-COPY-2">
177 <date>1998-05-04</date>
179 <title>Format of output files</title>
182 <date>1998-05-04</date>
184 <title>ASCII copy format</title>
186 When <command>COPY</command> is used without <function>BINARY</function>,
187 the file generated will have each instance on a single line, with each
188 attribute separated by the delimiter character. Embedded
189 delimiter characters will be preceded by a backslash character
190 (\). The attribute values themselves are strings generated by the
191 output function associated with each attribute type. The output
192 function for a type should not try to generate the backslash
193 character; this will be handled by <command>COPY</command> itself.
196 The actual format for each instance is
198 <attr1><<replaceable class=parameter>separator</replaceable>><attr2><<replaceable class=parameter>separator</replaceable>>...<<replaceable class=parameter>separator</replaceable>><attr<replaceable class="parameter">n</replaceable>><newline></programlisting>
199 The oid is placed on the beginning of the line
200 if <function>WITH OIDS</function> is specified.
203 If <command>COPY</command> is sending its output to standard
204 output instead of a file, it will send a backslash(\) and a period
205 (.) followed immediately by a newline, on a separate line,
206 when it is done. Similarly, if <command>COPY</command> is reading
207 from standard input, it will expect a backslash (\) and a period
208 (.) followed by a newline, as the first three characters on a
209 line, to denote end-of-file. However, <command>COPY</command>
210 will terminate (followed by the backend itself) if a true EOF is
214 The backslash character has special meaning. NULL attributes are
215 output as \N. A literal backslash character is output as two
216 consecutive backslashes. A literal tab character is represented
217 as a backslash and a tab. A literal newline character is
218 represented as a backslash and a newline. When loading ASCII data
219 not generated by PostgreSQL, you will need to convert backslash
220 characters (\) to double-backslashes (\\) to ensure that they are loaded
226 <date>1998-05-04</date>
228 <title>Binary copy format</title>
230 In the case of <command>COPY BINARY</command>, the first four
231 bytes in the file will be the number of instances in the file. If
232 this number is zero, the <command>COPY BINARY</command> command
233 will read until end of file is encountered. Otherwise, it will
234 stop reading when this number of instances has been read.
235 Remaining data in the file will be ignored.
238 The format for each instance in the file is as follows. Note that
239 this format must be followed <emphasis>exactly</emphasis>.
240 Unsigned four-byte integer quantities are called uint32 in the
244 <title>Contents of a binary copy file</title>
245 <tgroup cols="2"colsep="1" rowsep="1" align="center">
246 <COLSPEC COLNAME="col1">
247 <COLSPEC COLNAME="col2">
248 <spanspec namest="col1" nameend="col2" spanname="subhead">
251 <entry align="center" spanname="subhead">At the start of the file</entry>
254 <entry>uint32</entry>
255 <entry>number of tuples</entry>
258 <entry align="center" spanname="subhead">For each tuple</entry>
261 <entry>uint32</entry>
262 <entry>total length of tuple data</entry>
265 <entry>uint32</entry>
266 <entry>oid (if specified)</entry>
269 <entry>uint32</entry>
270 <entry>number of null attributes</entry>
273 <entry>[uint32</entry>
274 <entry>attribute number of first null attribute, counting from 0</entry>
281 <entry>uint32</entry>
282 <entry>attribute number of last null attribute]</entry>
286 <entry><tuple data></entry>
295 <date>1998-05-04</date>
297 <title>Alignment of binary data</title>
299 On Sun-3s, 2-byte attributes are aligned on two-byte boundaries,
300 and all larger attributes are aligned on four-byte boundaries.
301 Character attributes are aligned on single-byte boundaries. On
302 other machines, all attributes larger than 1 byte are aligned on
303 four-byte boundaries. Note that variable length attributes are
304 preceded by the attribute's length; arrays are simply contiguous
305 streams of the array element type.
311 <REFSECT1 ID="R1-SQL-COPY-3">
316 To copy a table to standard output, using | as a delimiter
319 COPY country TO stdout USING DELIMITERS '|';
322 To copy data from a Unix file into a table:
325 COPY country FROM '/usr1/proj/bray/sql/country_data';
328 A sample of data suitable for copying into a table from <filename>stdin</filename> (so it
329 has the termination sequence on the last line):
341 The same data, output in binary format on a Linux Intel machine.
342 The data is shown after filtering through the Unix utility <command>od -c</command>. The table has
343 three fields; the first is <classname>char(2)</classname> and the second is <classname>text</classname>. All the
344 rows have a null value in the third field). Notice how the <classname>char(2)</classname>
345 field is padded with nulls to four bytes and the text field is
346 preceded by its length:
349 355 \0 \0 \0 027 \0 \0 \0 001 \0 \0 \0 002 \0 \0 \0
350 006 \0 \0 \0 A F \0 \0 017 \0 \0 \0 A F G H
351 A N I S T A N 023 \0 \0 \0 001 \0 \0 \0 002
352 \0 \0 \0 006 \0 \0 \0 A L \0 \0 \v \0 \0 \0 A
353 L B A N I A 023 \0 \0 \0 001 \0 \0 \0 002 \0
354 \0 \0 006 \0 \0 \0 D Z \0 \0 \v \0 \0 \0 A L
356 ... \n \0 \0 \0 Z A M B I A 024 \0
357 \0 \0 001 \0 \0 \0 002 \0 \0 \0 006 \0 \0 \0 Z W
358 \0 \0 \f \0 \0 \0 Z I M B A B W E
362 <refsect1 ID="R1-SQL-COPY-4">
363 <title>See also</title>
365 insert(l), create table(l), vacuum(l), libpq.
369 <refsect1 ID="R1-SQL-COPY-5">
372 <command>COPY</command> stops operation at the first error. This
373 should not lead to problems in the event of a copy from, but the
374 target relation will, of course, be partially modified in a copy
375 to. The <command>VACUUM</command> query should be used to clean up
379 Because Postgres' current directory is not the same as the user's
380 working directory, the result of copying to a file "foo" (without
381 additional path information) may yield unexpected results for the
382 naive user. In this case, "foo" will wind up in $PGDATA/foo. In
383 general, the full pathname should be used when specifying files to
387 Files used as arguments to the copy command must reside on or be
388 accessible to the database server machine by being either on
389 local disks or on a networked file system.
392 When a TCP/IP connection from one machine to another is used, and a
393 target file is specified, the target file will be written on the
394 machine where the backend is running rather than the user's
399 <REFSECT1 ID="R1-SQL-COPY-6">
406 <REFSECT2 ID="R2-SQL-COPY-4">
408 <DATE>1998-04-15</DATE>
414 There is no COPY statement in SQL92.
420 <!-- Keep this comment at the end of the file
425 sgml-minimize-attributes:nil
426 sgml-always-quote-attributes:t
429 sgml-parent-document:nil
430 sgml-default-dtd-file:"../reference.ced"
431 sgml-exposed-tags:nil
432 sgml-local-catalogs:"/usr/lib/sgml/catalog"
433 sgml-local-ecat-files:nil