3 PostgreSQL documentation
7 <refentry id=
"SQL-COPY">
9 <refentrytitle id=
"sql-copy-title">COPY
</refentrytitle>
10 <manvolnum>7</manvolnum>
11 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
15 <refname>COPY
</refname>
16 <refpurpose>copy data between a file and a table
</refpurpose>
19 <indexterm zone=
"sql-copy">
20 <primary>COPY
</primary>
25 COPY
<replaceable class=
"parameter">tablename
</replaceable> [ (
<replaceable class=
"parameter">column
</replaceable> [, ...] ) ]
26 FROM { '
<replaceable class=
"parameter">filename
</replaceable>' | STDIN }
30 [ DELIMITER [ AS ] '
<replaceable class=
"parameter">delimiter
</replaceable>' ]
31 [ NULL [ AS ] '
<replaceable class=
"parameter">null string
</replaceable>' ]
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> [, ...] ]
37 COPY {
<replaceable class=
"parameter">tablename
</replaceable> [ (
<replaceable class=
"parameter">column
</replaceable> [, ...] ) ] | (
<replaceable class=
"parameter">query
</replaceable> ) }
38 TO { '
<replaceable class=
"parameter">filename
</replaceable>' | STDOUT }
42 [ DELIMITER [ AS ] '
<replaceable class=
"parameter">delimiter
</replaceable>' ]
43 [ NULL [ AS ] '
<replaceable class=
"parameter">null string
</replaceable>' ]
45 [ QUOTE [ AS ] '
<replaceable class=
"parameter">quote
</replaceable>' ]
46 [ ESCAPE [ AS ] '
<replaceable class=
"parameter">escape
</replaceable>' ]
47 [ FORCE QUOTE
<replaceable class=
"parameter">column
</replaceable> [, ...] ]
52 <title>Description
</title>
55 <command>COPY
</command> moves data between
56 <productname>PostgreSQL
</productname> tables and standard file-system
57 files.
<command>COPY TO
</command> copies the contents of a table
58 <emphasis>to<
/> a file, while
<command>COPY FROM
</command> copies
59 data
<emphasis>from<
/> a file to a table (appending the data to
60 whatever is in the table already).
<command>COPY TO
</command>
61 can also copy the results of a
<command>SELECT<
/> query.
65 If a list of columns is specified,
<command>COPY
</command> will
66 only copy the data in the specified columns to or from the file.
67 If there are any columns in the table that are not in the column list,
68 <command>COPY FROM
</command> will insert the default values for
73 <command>COPY
</command> with a file name instructs the
74 <productname>PostgreSQL
</productname> server to directly read from
75 or write to a file. The file must be accessible to the server and
76 the name must be specified from the viewpoint of the server. When
77 <literal>STDIN
</literal> or
<literal>STDOUT
</literal> is
78 specified, data is transmitted via the connection between the
79 client and the server.
84 <title>Parameters
</title>
88 <term><replaceable class=
"parameter">tablename
</replaceable></term>
91 The name (optionally schema-qualified) of an existing table.
97 <term><replaceable class=
"parameter">column
</replaceable></term>
100 An optional list of columns to be copied. If no column list is
101 specified, all columns of the table will be copied.
107 <term><replaceable class=
"parameter">query
</replaceable></term>
110 A
<xref linkend=
"sql-select" endterm=
"sql-select-title"> or
111 <xref linkend=
"sql-values" endterm=
"sql-values-title"> command
112 whose results are to be copied.
113 Note that parentheses are required around the query.
119 <term><replaceable class=
"parameter">filename
</replaceable></term>
122 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.
130 <term><literal>STDIN
</literal></term>
133 Specifies that input comes from the client application.
139 <term><literal>STDOUT
</literal></term>
142 Specifies that output goes to the client application.
148 <term><literal>BINARY
</literal></term>
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.
159 <term><literal>OIDS
</literal></term>
162 Specifies copying the OID for each row. (An error is raised if
163 <literal>OIDS
</literal> is specified for a table that does not
164 have OIDs, or in the case of copying a
<replaceable
165 class=
"parameter">query
</replaceable>.)
171 <term><replaceable class=
"parameter">delimiter
</replaceable></term>
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.
182 <term><replaceable class=
"parameter">null string
</replaceable></term>
185 The string that represents a null value. The default is
186 <literal>\N
</literal> (backslash-N) in text mode, and a empty
187 value with no quotes in
<literal>CSV<
/> mode. You might prefer an
188 empty string even in text mode for cases where you don't want to
189 distinguish nulls from empty strings.
194 When using
<command>COPY FROM
</command>, any data item that matches
195 this string will be stored as a null value, so you should make
196 sure that you use the same string as you used with
197 <command>COPY TO
</command>.
205 <term><literal>CSV
</literal></term>
208 Selects Comma Separated Value (
<literal>CSV<
/>) mode.
214 <term><literal>HEADER
</literal></term>
217 Specifies that the file contains a header line with the names of each
218 column in the file. On output, the first line contains the column
219 names from the table, and on input, the first line is ignored.
225 <term><replaceable class=
"parameter">quote
</replaceable></term>
228 Specifies the ASCII quotation character in
<literal>CSV<
/> mode.
229 The default is double-quote.
235 <term><replaceable class=
"parameter">escape
</replaceable></term>
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).
246 <term><literal>FORCE QUOTE<
/></term>
249 In
<literal>CSV<
/> <command>COPY TO<
/> mode, forces quoting to be
250 used for all non-
<literal>NULL<
/> values in each specified column.
251 <literal>NULL<
/> output is never quoted.
257 <term><literal>FORCE NOT NULL<
/></term>
260 In
<literal>CSV<
/> <command>COPY FROM<
/> mode, process each
261 specified column as though it were quoted and hence not a
262 <literal>NULL<
/> value. For the default null string in
263 <literal>CSV<
/> mode (
<literal>''<
/>), this causes missing
264 values to be input as zero-length strings.
273 <title>Outputs
</title>
276 On successful completion, a
<command>COPY<
/> command returns a command
279 COPY
<replaceable class=
"parameter">count
</replaceable>
281 The
<replaceable class=
"parameter">count
</replaceable> is the number
290 <command>COPY
</command> can only be used with plain tables, not
291 with views. However, you can write
<literal>COPY (SELECT * FROM
292 <replaceable class=
"parameter">viewname
</replaceable>) TO ...
</literal>.
296 The
<literal>BINARY
</literal> key word causes all data to be
297 stored/read as binary format rather than as text. It is
298 somewhat faster than the normal text mode, but a binary-format
299 file is less portable across machine architectures and
300 <productname>PostgreSQL
</productname> versions.
304 You must have select privilege on the table
305 whose values are read by
<command>COPY TO
</command>, and
306 insert privilege on the table into which values
307 are inserted by
<command>COPY FROM
</command>. It is sufficient
308 to have column privileges on the column(s) listed in the command.
312 Files named in a
<command>COPY
</command> command are read or written
313 directly by the server, not by the client application. Therefore,
314 they must reside on or be accessible to the database server machine,
315 not the client. They must be accessible to and readable or writable
316 by the
<productname>PostgreSQL
</productname> user (the user ID the
317 server runs as), not the client.
<command>COPY
</command> naming a
318 file is only allowed to database superusers, since it allows reading
319 or writing any file that the server has privileges to access.
323 Do not confuse
<command>COPY
</command> with the
324 <application>psql
</application> instruction
325 <command>\copy
</command>.
<command>\copy
</command> invokes
326 <command>COPY FROM STDIN
</command> or
<command>COPY TO
327 STDOUT
</command>, and then fetches/stores the data in a file
328 accessible to the
<application>psql
</application> client. Thus,
329 file accessibility and access rights depend on the client rather
330 than the server when
<command>\copy
</command> is used.
334 It is recommended that the file name used in
<command>COPY
</command>
335 always be specified as an absolute path. This is enforced by the
336 server in the case of
<command>COPY TO
</command>, but for
337 <command>COPY FROM
</command> you do have the option of reading from
338 a file specified by a relative path. The path will be interpreted
339 relative to the working directory of the server process (normally
340 the cluster's data directory), not the client's working directory.
344 <command>COPY FROM
</command> will invoke any triggers and check
345 constraints on the destination table. However, it will not invoke rules.
349 <command>COPY
</command> input and output is affected by
350 <varname>DateStyle
</varname>. To ensure portability to other
351 <productname>PostgreSQL
</productname> installations that might use
352 non-default
<varname>DateStyle
</varname> settings,
353 <varname>DateStyle
</varname> should be set to
<literal>ISO<
/> before
354 using
<command>COPY TO<
/>. It is also a good idea to avoid dumping
355 data with
<varname>IntervalStyle
</varname> set to
356 <literal>sql_standard<
/>, because negative interval values might be
357 misinterpreted by a server that has a different setting for
358 <varname>IntervalStyle
</varname>.
362 Input data is interpreted according to the current client encoding,
363 and output data is encoded in the the current client encoding, even
364 if the data does not pass through the client but is read from or
369 <command>COPY
</command> stops operation at the first error. This
370 should not lead to problems in the event of a
<command>COPY
371 TO
</command>, but the target table will already have received
372 earlier rows in a
<command>COPY FROM
</command>. These rows will not
373 be visible or accessible, but they still occupy disk space. This might
374 amount to a considerable amount of wasted disk space if the failure
375 happened well into a large copy operation. You might wish to invoke
376 <command>VACUUM
</command> to recover the wasted space.
382 <title>File Formats
</title>
385 <title>Text Format
</title>
388 When
<command>COPY
</command> is used without the
<literal>BINARY
</literal>
389 or
<literal>CSV<
/> options,
390 the data read or written is a text file with one line per table row.
391 Columns in a row are separated by the delimiter character.
392 The column values themselves are strings generated by the
393 output function, or acceptable to the input function, of each
394 attribute's data type. The specified null string is used in
395 place of columns that are null.
396 <command>COPY FROM
</command> will raise an error if any line of the
397 input file contains more or fewer columns than are expected.
398 If
<literal>OIDS
</literal> is specified, the OID is read or written as the first column,
399 preceding the user data columns.
403 End of data can be represented by a single line containing just
404 backslash-period (
<literal>\.<
/>). An end-of-data marker is
405 not necessary when reading from a file, since the end of file
406 serves perfectly well; it is needed only when copying data to or from
407 client applications using pre-
3.0 client protocol.
411 Backslash characters (
<literal>\<
/>) can be used in the
412 <command>COPY
</command> data to quote data characters that might
413 otherwise be taken as row or column delimiters. In particular, the
414 following characters
<emphasis>must<
/> be preceded by a backslash if
415 they appear as part of a column value: backslash itself,
416 newline, carriage return, and the current delimiter character.
420 The specified null string is sent by
<command>COPY TO
</command> without
421 adding any backslashes; conversely,
<command>COPY FROM
</command> matches
422 the input against the null string before removing backslashes. Therefore,
423 a null string such as
<literal>\N
</literal> cannot be confused with
424 the actual data value
<literal>\N
</literal> (which would be represented
425 as
<literal>\\N
</literal>).
429 The following special backslash sequences are recognized by
430 <command>COPY FROM
</command>:
436 <entry>Sequence
</entry>
437 <entry>Represents
</entry>
443 <entry><literal>\b<
/></entry>
444 <entry>Backspace (ASCII
8)
</entry>
447 <entry><literal>\f<
/></entry>
448 <entry>Form feed (ASCII
12)
</entry>
451 <entry><literal>\n<
/></entry>
452 <entry>Newline (ASCII
10)
</entry>
455 <entry><literal>\r<
/></entry>
456 <entry>Carriage return (ASCII
13)
</entry>
459 <entry><literal>\t<
/></entry>
460 <entry>Tab (ASCII
9)
</entry>
463 <entry><literal>\v<
/></entry>
464 <entry>Vertical tab (ASCII
11)
</entry>
467 <entry><literal>\<
/><replaceable>digits<
/></entry>
468 <entry>Backslash followed by one to three octal digits specifies
469 the character with that numeric code
</entry>
472 <entry><literal>\x<
/><replaceable>digits<
/></entry>
473 <entry>Backslash
<literal>x<
/> followed by one or two hex digits specifies
474 the character with that numeric code
</entry>
480 Presently,
<command>COPY TO
</command> will never emit an octal or
481 hex-digits backslash sequence, but it does use the other sequences
482 listed above for those control characters.
486 Any other backslashed character that is not mentioned in the above table
487 will be taken to represent itself. However, beware of adding backslashes
488 unnecessarily, since that might accidentally produce a string matching the
489 end-of-data marker (
<literal>\.<
/>) or the null string (
<literal>\N<
/> by
490 default). These strings will be recognized before any other backslash
495 It is strongly recommended that applications generating
<command>COPY
</command> data convert
496 data newlines and carriage returns to the
<literal>\n<
/> and
497 <literal>\r<
/> sequences respectively. At present it is
498 possible to represent a data carriage return by a backslash and carriage
499 return, and to represent a data newline by a backslash and newline.
500 However, these representations might not be accepted in future releases.
501 They are also highly vulnerable to corruption if the
<command>COPY
</command> file is
502 transferred across different machines (for example, from Unix to Windows
507 <command>COPY TO
</command> will terminate each row with a Unix-style
508 newline (
<quote><literal>\n<
/><
/>). Servers running on Microsoft Windows instead
509 output carriage return/newline (
<quote><literal>\r\n<
/><
/>), but only for
510 <command>COPY<
/> to a server file; for consistency across platforms,
511 <command>COPY TO STDOUT<
/> always sends
<quote><literal>\n<
/><
/>
512 regardless of server platform.
513 <command>COPY FROM
</command> can handle lines ending with newlines,
514 carriage returns, or carriage return/newlines. To reduce the risk of
515 error due to un-backslashed newlines or carriage returns that were
516 meant as data,
<command>COPY FROM
</command> will complain if the line
517 endings in the input are not all alike.
522 <title>CSV Format
</title>
525 This format is used for importing and exporting the Comma
526 Separated Value (
<literal>CSV<
/>) file format used by many other
527 programs, such as spreadsheets. Instead of the escaping used by
528 <productname>PostgreSQL
</productname>'s standard text mode, it
529 produces and recognizes the common CSV escaping mechanism.
533 The values in each record are separated by the
<literal>DELIMITER<
/>
534 character. If the value contains the delimiter character, the
535 <literal>QUOTE<
/> character, the
<literal>NULL<
/> string, a carriage
536 return, or line feed character, then the whole value is prefixed and
537 suffixed by the
<literal>QUOTE<
/> character, and any occurrence
538 within the value of a
<literal>QUOTE<
/> character or the
539 <literal>ESCAPE<
/> character is preceded by the escape character.
540 You can also use
<literal>FORCE QUOTE<
/> to force quotes when outputting
541 non-
<literal>NULL<
/> values in specific columns.
545 The
<literal>CSV<
/> format has no standard way to distinguish a
546 <literal>NULL<
/> value from an empty string.
547 <productname>PostgreSQL<
/>'s
<command>COPY<
/> handles this by
548 quoting. A
<literal>NULL<
/> is output as the
<literal>NULL<
/>
549 string and is not quoted, while a data value matching the
550 <literal>NULL<
/> string is quoted. Therefore, using the default
551 settings, a
<literal>NULL<
/> is written as an unquoted empty
552 string, while an empty string is written with double quotes
553 (
<literal>""<
/>). Reading values follows similar rules. You can
554 use
<literal>FORCE NOT NULL<
/> to prevent
<literal>NULL<
/> input
555 comparisons for specific columns.
559 Because backslash is not a special character in the
<literal>CSV<
/>
560 format,
<literal>\.<
/>, the end-of-data marker, could also appear
561 as a data value. To avoid any misinterpretation, a
<literal>\.<
/>
562 data value appearing as a lone entry on a line is automatically
563 quoted on output, and on input, if quoted, is not interpreted as the
564 end-of-data marker. If you are loading a file created by another
565 application that has a single unquoted column and might have a
566 value of
<literal>\.<
/>, you might need to quote that value in the
572 In
<literal>CSV<
/> mode, all characters are significant. A quoted value
573 surrounded by white space, or any characters other than
574 <literal>DELIMITER<
/>, will include those characters. This can cause
575 errors if you import data from a system that pads
<literal>CSV<
/>
576 lines with white space out to some fixed width. If such a situation
577 arises you might need to preprocess the
<literal>CSV<
/> file to remove
578 the trailing white space, before importing the data into
579 <productname>PostgreSQL<
/>.
585 CSV mode will both recognize and produce CSV files with quoted
586 values containing embedded carriage returns and line feeds. Thus
587 the files are not strictly one line per table row like text-mode
594 Many programs produce strange and occasionally perverse CSV files,
595 so the file format is more a convention than a standard. Thus you
596 might encounter some files that cannot be imported using this
597 mechanism, and
<command>COPY<
/> might produce files that other
598 programs cannot process.
605 <title>Binary Format
</title>
608 The file format used for
<command>COPY BINARY
</command> changed in
609 <productname>PostgreSQL
</productname> 7.4. The new format consists
610 of a file header, zero or more tuples containing the row data, and
611 a file trailer. Headers and data are now in network byte order.
615 <title>File Header
</title>
618 The file header consists of
15 bytes of fixed fields, followed
619 by a variable-length header extension area. The fixed fields are:
623 <term>Signature
</term>
626 11-byte sequence
<literal>PGCOPY\n\
377\r\n\
0<
/> — note that the zero byte
627 is a required part of the signature. (The signature is designed to allow
628 easy identification of files that have been munged by a non-
8-bit-clean
629 transfer. This signature will be changed by end-of-line-translation
630 filters, dropped zero bytes, dropped high bits, or parity changes.)
636 <term>Flags field
</term>
639 32-bit integer bit mask to denote important aspects of the file format. Bits
640 are numbered from
0 (
<acronym>LSB<
/>) to
31 (
<acronym>MSB<
/>). Note that
641 this field is stored in network byte order (most significant byte first),
642 as are all the integer fields used in the file format. Bits
643 16-
31 are reserved to denote critical file format issues; a reader
644 should abort if it finds an unexpected bit set in this range. Bits
0-
15
645 are reserved to signal backwards-compatible format issues; a reader
646 should simply ignore any unexpected bits set in this range. Currently
647 only one flag bit is defined, and the rest must be zero:
653 if
1, OIDs are included in the data; if
0, not
663 <term>Header extension area length
</term>
666 32-bit integer, length in bytes of remainder of header, not including self.
667 Currently, this is zero, and the first tuple follows
668 immediately. Future changes to the format might allow additional data
669 to be present in the header. A reader should silently skip over any header
670 extension data it does not know what to do with.
678 The header extension area is envisioned to contain a sequence of
679 self-identifying chunks. The flags field is not intended to tell readers
680 what is in the extension area. Specific design of header extension contents
681 is left for a later release.
685 This design allows for both backwards-compatible header additions (add
686 header extension chunks, or set low-order flag bits) and
687 non-backwards-compatible changes (set high-order flag bits to signal such
688 changes, and add supporting data to the extension area if needed).
693 <title>Tuples
</title>
695 Each tuple begins with a
16-bit integer count of the number of fields in the
696 tuple. (Presently, all tuples in a table will have the same count, but that
697 might not always be true.) Then, repeated for each field in the tuple, there
698 is a
32-bit length word followed by that many bytes of field data. (The
699 length word does not include itself, and can be zero.) As a special case,
700 -
1 indicates a NULL field value. No value bytes follow in the NULL case.
704 There is no alignment padding or any other extra data between fields.
708 Presently, all data values in a
<command>COPY BINARY
</command> file are
709 assumed to be in binary format (format code one). It is anticipated that a
710 future extension might add a header field that allows per-column format codes
715 To determine the appropriate binary format for the actual tuple data you
716 should consult the
<productname>PostgreSQL
</productname> source, in
717 particular the
<function>*send<
/> and
<function>*recv<
/> functions for
718 each column's data type (typically these functions are found in the
719 <filename>src/backend/utils/adt/
</filename> directory of the source
724 If OIDs are included in the file, the OID field immediately follows the
725 field-count word. It is a normal field except that it's not included
726 in the field-count. In particular it has a length word
— this will allow
727 handling of
4-byte vs.
8-byte OIDs without too much pain, and will allow
728 OIDs to be shown as null if that ever proves desirable.
733 <title>File Trailer
</title>
736 The file trailer consists of a
16-bit integer word containing -
1. This
737 is easily distinguished from a tuple's field-count word.
741 A reader should report an error if a field-count word is neither -
1
742 nor the expected number of columns. This provides an extra
743 check against somehow getting out of sync with the data.
750 <title>Examples
</title>
753 The following example copies a table to the client
754 using the vertical bar (
<literal>|
</literal>) as the field delimiter:
756 COPY country TO STDOUT WITH DELIMITER '|';
761 To copy data from a file into the
<literal>country<
/> table:
763 COPY country FROM '/usr1/proj/bray/sql/country_data';
768 To copy into a file just the countries whose names start with 'A':
770 COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';
775 Here is a sample of data suitable for copying into a table from
776 <literal>STDIN
</literal>:
784 Note that the white space on each line is actually a tab character.
788 The following is the same data, output in binary format.
789 The data is shown after filtering through the
790 Unix utility
<command>od -c
</command>. The table has three columns;
791 the first has type
<type>char(
2)
</type>, the second has type
<type>text
</type>,
792 and the third has type
<type>integer
</type>. All the rows have a null value
795 0000000 P G C O P Y \n
377 \r \n \
0 \
0 \
0 \
0 \
0 \
0
796 0000020 \
0 \
0 \
0 \
0 003 \
0 \
0 \
0 002 A F \
0 \
0 \
0 013 A
797 0000040 F G H A N I S T A N
377 377 377 377 \
0 003
798 0000060 \
0 \
0 \
0 002 A L \
0 \
0 \
0 007 A L B A N I
799 0000100 A
377 377 377 377 \
0 003 \
0 \
0 \
0 002 D Z \
0 \
0 \
0
800 0000120 007 A L G E R I A
377 377 377 377 \
0 003 \
0 \
0
801 0000140 \
0 002 Z M \
0 \
0 \
0 006 Z A M B I A
377 377
802 0000160 377 377 \
0 003 \
0 \
0 \
0 002 Z W \
0 \
0 \
0 \b Z I
803 0000200 M B A B W E
377 377 377 377 377 377
809 <title>Compatibility
</title>
812 There is no
<command>COPY
</command> statement in the SQL standard.
816 The following syntax was used before
<productname>PostgreSQL<
/>
817 version
7.3 and is still supported:
820 COPY [ BINARY ]
<replaceable class=
"parameter">tablename
</replaceable> [ WITH OIDS ]
821 FROM { '
<replaceable class=
"parameter">filename
</replaceable>' | STDIN }
822 [ [USING] DELIMITERS '
<replaceable class=
"parameter">delimiter
</replaceable>' ]
823 [ WITH NULL AS '
<replaceable class=
"parameter">null string
</replaceable>' ]
825 COPY [ BINARY ]
<replaceable class=
"parameter">tablename
</replaceable> [ WITH OIDS ]
826 TO { '
<replaceable class=
"parameter">filename
</replaceable>' | STDOUT }
827 [ [USING] DELIMITERS '
<replaceable class=
"parameter">delimiter
</replaceable>' ]
828 [ WITH NULL AS '
<replaceable class=
"parameter">null string
</replaceable>' ]