2 doc/src/sgml/ref/copy.sgml
3 PostgreSQL documentation
7 <refentry id="sql-copy">
8 <indexterm zone="sql-copy">
9 <primary>COPY</primary>
13 <refentrytitle>COPY</refentrytitle>
14 <manvolnum>7</manvolnum>
15 <refmiscinfo>SQL - Language Statements</refmiscinfo>
19 <refname>COPY</refname>
20 <refpurpose>copy data between a file and a table</refpurpose>
25 COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
26 FROM { '<replaceable class="parameter">filename</replaceable>' | PROGRAM '<replaceable class="parameter">command</replaceable>' | STDIN }
27 [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
28 [ WHERE <replaceable class="parameter">condition</replaceable> ]
30 COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
31 TO { '<replaceable class="parameter">filename</replaceable>' | PROGRAM '<replaceable class="parameter">command</replaceable>' | STDOUT }
32 [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
34 <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
36 FORMAT <replaceable class="parameter">format_name</replaceable>
37 FREEZE [ <replaceable class="parameter">boolean</replaceable> ]
38 DELIMITER '<replaceable class="parameter">delimiter_character</replaceable>'
39 NULL '<replaceable class="parameter">null_string</replaceable>'
40 HEADER [ <replaceable class="parameter">boolean</replaceable> ]
41 QUOTE '<replaceable class="parameter">quote_character</replaceable>'
42 ESCAPE '<replaceable class="parameter">escape_character</replaceable>'
43 FORCE_QUOTE { ( <replaceable class="parameter">column_name</replaceable> [, ...] ) | * }
44 FORCE_NOT_NULL ( <replaceable class="parameter">column_name</replaceable> [, ...] )
45 FORCE_NULL ( <replaceable class="parameter">column_name</replaceable> [, ...] )
46 ENCODING '<replaceable class="parameter">encoding_name</replaceable>'
51 <title>Description</title>
54 <command>COPY</command> moves data between
55 <productname>PostgreSQL</productname> tables and standard file-system
56 files. <command>COPY TO</command> copies the contents of a table
57 <emphasis>to</emphasis> a file, while <command>COPY FROM</command> copies
58 data <emphasis>from</emphasis> a file to a table (appending the data to
59 whatever is in the table already). <command>COPY TO</command>
60 can also copy the results of a <command>SELECT</command> query.
64 If a list of columns is specified, <command>COPY</command> will
65 only copy the data in the specified columns to or from the file.
66 If there are any columns in the table that are not in the column list,
67 <command>COPY FROM</command> will insert the default values for
72 <command>COPY</command> with a file name instructs the
73 <productname>PostgreSQL</productname> server to directly read from
74 or write to a file. The file must be accessible by the
75 <productname>PostgreSQL</productname> user (the user ID the server
76 runs as) and the name must be specified from the viewpoint of the
77 server. When <literal>PROGRAM</literal> is specified, the server
78 executes the given command and reads from the standard output of the
79 program, or writes to the standard input of the program. The command
80 must be specified from the viewpoint of the server, and be executable
81 by the <productname>PostgreSQL</productname> user. When
82 <literal>STDIN</literal> or <literal>STDOUT</literal> is
83 specified, data is transmitted via the connection between the
84 client and the server.
89 <title>Parameters</title>
93 <term><replaceable class="parameter">table_name</replaceable></term>
96 The name (optionally schema-qualified) of an existing table.
102 <term><replaceable class="parameter">column_name</replaceable></term>
105 An optional list of columns to be copied. If no column list is
106 specified, all columns of the table except generated columns will be
113 <term><replaceable class="parameter">query</replaceable></term>
116 A <xref linkend="sql-select"/>, <xref linkend="sql-values"/>,
117 <xref linkend="sql-insert"/>, <xref linkend="sql-update"/> or
118 <xref linkend="sql-delete"/> command whose results are to be
119 copied. Note that parentheses are required around the query.
122 For <command>INSERT</command>, <command>UPDATE</command> and
123 <command>DELETE</command> queries a RETURNING clause must be provided,
124 and the target relation must not have a conditional rule, nor
125 an <literal>ALSO</literal> rule, nor an <literal>INSTEAD</literal> rule
126 that expands to multiple statements.
132 <term><replaceable class="parameter">filename</replaceable></term>
135 The path name of the input or output file. An input file name can be
136 an absolute or relative path, but an output file name must be an absolute
137 path. Windows users might need to use an <literal>E''</literal> string and
138 double any backslashes used in the path name.
144 <term><literal>PROGRAM</literal></term>
147 A command to execute. In <command>COPY FROM</command>, the input is
148 read from standard output of the command, and in <command>COPY TO</command>,
149 the output is written to the standard input of the command.
152 Note that the command is invoked by the shell, so if you need to pass
153 any arguments to shell command that come from an untrusted source, you
154 must be careful to strip or escape any special characters that might
155 have a special meaning for the shell. For security reasons, it is best
156 to use a fixed command string, or at least avoid passing any user input
163 <term><literal>STDIN</literal></term>
166 Specifies that input comes from the client application.
172 <term><literal>STDOUT</literal></term>
175 Specifies that output goes to the client application.
181 <term><replaceable class="parameter">boolean</replaceable></term>
184 Specifies whether the selected option should be turned on or off.
185 You can write <literal>TRUE</literal>, <literal>ON</literal>, or
186 <literal>1</literal> to enable the option, and <literal>FALSE</literal>,
187 <literal>OFF</literal>, or <literal>0</literal> to disable it. The
188 <replaceable class="parameter">boolean</replaceable> value can also
189 be omitted, in which case <literal>TRUE</literal> is assumed.
195 <term><literal>FORMAT</literal></term>
198 Selects the data format to be read or written:
199 <literal>text</literal>,
200 <literal>csv</literal> (Comma Separated Values),
201 or <literal>binary</literal>.
202 The default is <literal>text</literal>.
208 <term><literal>FREEZE</literal></term>
211 Requests copying the data with rows already frozen, just as they
212 would be after running the <command>VACUUM FREEZE</command> command.
213 This is intended as a performance option for initial data loading.
214 Rows will be frozen only if the table being loaded has been created
215 or truncated in the current subtransaction, there are no cursors
216 open and there are no older snapshots held by this transaction. It is
217 currently not possible to perform a <command>COPY FREEZE</command> on
221 Note that all other sessions will immediately be able to see the data
222 once it has been successfully loaded. This violates the normal rules
223 of MVCC visibility and users specifying should be aware of the
224 potential problems this might cause.
230 <term><literal>DELIMITER</literal></term>
233 Specifies the character that separates columns within each row
234 (line) of the file. The default is a tab character in text format,
235 a comma in <literal>CSV</literal> format.
236 This must be a single one-byte character.
237 This option is not allowed when using <literal>binary</literal> format.
243 <term><literal>NULL</literal></term>
246 Specifies the string that represents a null value. The default is
247 <literal>\N</literal> (backslash-N) in text format, and an unquoted empty
248 string in <literal>CSV</literal> format. You might prefer an
249 empty string even in text format for cases where you don't want to
250 distinguish nulls from empty strings.
251 This option is not allowed when using <literal>binary</literal> format.
256 When using <command>COPY FROM</command>, any data item that matches
257 this string will be stored as a null value, so you should make
258 sure that you use the same string as you used with
259 <command>COPY TO</command>.
267 <term><literal>HEADER</literal></term>
270 Specifies that the file contains a header line with the names of each
271 column in the file. On output, the first line contains the column
272 names from the table, and on input, the first line is ignored.
273 This option is allowed only when using <literal>CSV</literal> format.
279 <term><literal>QUOTE</literal></term>
282 Specifies the quoting character to be used when a data value is quoted.
283 The default is double-quote.
284 This must be a single one-byte character.
285 This option is allowed only when using <literal>CSV</literal> format.
291 <term><literal>ESCAPE</literal></term>
294 Specifies the character that should appear before a
295 data character that matches the <literal>QUOTE</literal> value.
296 The default is the same as the <literal>QUOTE</literal> value (so that
297 the quoting character is doubled if it appears in the data).
298 This must be a single one-byte character.
299 This option is allowed only when using <literal>CSV</literal> format.
305 <term><literal>FORCE_QUOTE</literal></term>
309 used for all non-<literal>NULL</literal> values in each specified column.
310 <literal>NULL</literal> output is never quoted. If <literal>*</literal> is specified,
311 non-<literal>NULL</literal> values will be quoted in all columns.
312 This option is allowed only in <command>COPY TO</command>, and only when
313 using <literal>CSV</literal> format.
319 <term><literal>FORCE_NOT_NULL</literal></term>
322 Do not match the specified columns' values against the null string.
323 In the default case where the null string is empty, this means that
324 empty values will be read as zero-length strings rather than nulls,
325 even when they are not quoted.
326 This option is allowed only in <command>COPY FROM</command>, and only when
327 using <literal>CSV</literal> format.
333 <term><literal>FORCE_NULL</literal></term>
336 Match the specified columns' values against the null string, even
337 if it has been quoted, and if a match is found set the value to
338 <literal>NULL</literal>. In the default case where the null string is empty,
339 this converts a quoted empty string into NULL.
340 This option is allowed only in <command>COPY FROM</command>, and only when
341 using <literal>CSV</literal> format.
347 <term><literal>ENCODING</literal></term>
350 Specifies that the file is encoded in the <replaceable
351 class="parameter">encoding_name</replaceable>. If this option is
352 omitted, the current client encoding is used. See the Notes below
359 <term><literal>WHERE</literal></term>
362 The optional <literal>WHERE</literal> clause has the general form
364 WHERE <replaceable class="parameter">condition</replaceable>
366 where <replaceable class="parameter">condition</replaceable> is
367 any expression that evaluates to a result of type
368 <type>boolean</type>. Any row that does not satisfy this
369 condition will not be inserted to the table. A row satisfies the
370 condition if it returns true when the actual row values are
371 substituted for any variable references.
375 Currently, subqueries are not allowed in <literal>WHERE</literal>
376 expressions, and the evaluation does not see any changes made by the
377 <command>COPY</command> itself (this matters when the expression
378 contains calls to <literal>VOLATILE</literal> functions).
388 <title>Outputs</title>
391 On successful completion, a <command>COPY</command> command returns a command
394 COPY <replaceable class="parameter">count</replaceable>
396 The <replaceable class="parameter">count</replaceable> is the number
402 <application>psql</application> will print this command tag only if the command
403 was not <literal>COPY ... TO STDOUT</literal>, or the
404 equivalent <application>psql</application> meta-command
405 <literal>\copy ... to stdout</literal>. This is to prevent confusing the
406 command tag with the data that was just printed.
415 <command>COPY TO</command> can only be used with plain tables, not
416 with views. However, you can write <literal>COPY (SELECT * FROM
417 <replaceable class="parameter">viewname</replaceable>) TO ...</literal>
418 to copy the current contents of a view.
422 <command>COPY FROM</command> can be used with plain, foreign, or
423 partitioned tables or with views that have
424 <literal>INSTEAD OF INSERT</literal> triggers.
428 <command>COPY</command> only deals with the specific table named;
429 it does not copy data to or from child tables. Thus for example
430 <literal>COPY <replaceable class="parameter">table</replaceable> TO</literal>
431 shows the same data as <literal>SELECT * FROM ONLY <replaceable
432 class="parameter">table</replaceable></literal>. But <literal>COPY
433 (SELECT * FROM <replaceable class="parameter">table</replaceable>) TO ...</literal>
434 can be used to dump all of the data in an inheritance hierarchy.
438 You must have select privilege on the table
439 whose values are read by <command>COPY TO</command>, and
440 insert privilege on the table into which values
441 are inserted by <command>COPY FROM</command>. It is sufficient
442 to have column privileges on the column(s) listed in the command.
446 If row-level security is enabled for the table, the relevant
447 <command>SELECT</command> policies will apply to <literal>COPY
448 <replaceable class="parameter">table</replaceable> TO</literal> statements.
449 Currently, <command>COPY FROM</command> is not supported for tables
450 with row-level security. Use equivalent <command>INSERT</command>
455 Files named in a <command>COPY</command> command are read or written
456 directly by the server, not by the client application. Therefore,
457 they must reside on or be accessible to the database server machine,
458 not the client. They must be accessible to and readable or writable
459 by the <productname>PostgreSQL</productname> user (the user ID the
460 server runs as), not the client. Similarly,
461 the command specified with <literal>PROGRAM</literal> is executed directly
462 by the server, not by the client application, must be executable by the
463 <productname>PostgreSQL</productname> user.
464 <command>COPY</command> naming a file or command is only allowed to
465 database superusers or users who are granted one of the default roles
466 <literal>pg_read_server_files</literal>,
467 <literal>pg_write_server_files</literal>,
468 or <literal>pg_execute_server_program</literal>, since it allows reading
469 or writing any file or running a program that the server has privileges to
474 Do not confuse <command>COPY</command> with the
475 <application>psql</application> instruction
476 <command><link linkend="app-psql-meta-commands-copy">\copy</link></command>. <command>\copy</command> invokes
477 <command>COPY FROM STDIN</command> or <command>COPY TO
478 STDOUT</command>, and then fetches/stores the data in a file
479 accessible to the <application>psql</application> client. Thus,
480 file accessibility and access rights depend on the client rather
481 than the server when <command>\copy</command> is used.
485 It is recommended that the file name used in <command>COPY</command>
486 always be specified as an absolute path. This is enforced by the
487 server in the case of <command>COPY TO</command>, but for
488 <command>COPY FROM</command> you do have the option of reading from
489 a file specified by a relative path. The path will be interpreted
490 relative to the working directory of the server process (normally
491 the cluster's data directory), not the client's working directory.
495 Executing a command with <literal>PROGRAM</literal> might be restricted
496 by the operating system's access control mechanisms, such as SELinux.
500 <command>COPY FROM</command> will invoke any triggers and check
501 constraints on the destination table. However, it will not invoke rules.
505 For identity columns, the <command>COPY FROM</command> command will always
506 write the column values provided in the input data, like
507 the <command>INSERT</command> option <literal>OVERRIDING SYSTEM
512 <command>COPY</command> input and output is affected by
513 <varname>DateStyle</varname>. To ensure portability to other
514 <productname>PostgreSQL</productname> installations that might use
515 non-default <varname>DateStyle</varname> settings,
516 <varname>DateStyle</varname> should be set to <literal>ISO</literal> before
517 using <command>COPY TO</command>. It is also a good idea to avoid dumping
518 data with <varname>IntervalStyle</varname> set to
519 <literal>sql_standard</literal>, because negative interval values might be
520 misinterpreted by a server that has a different setting for
521 <varname>IntervalStyle</varname>.
525 Input data is interpreted according to <literal>ENCODING</literal>
526 option or the current client encoding, and output data is encoded
527 in <literal>ENCODING</literal> or the current client encoding, even
528 if the data does not pass through the client but is read from or
529 written to a file directly by the server.
533 <command>COPY</command> stops operation at the first error. This
534 should not lead to problems in the event of a <command>COPY
535 TO</command>, but the target table will already have received
536 earlier rows in a <command>COPY FROM</command>. These rows will not
537 be visible or accessible, but they still occupy disk space. This might
538 amount to a considerable amount of wasted disk space if the failure
539 happened well into a large copy operation. You might wish to invoke
540 <command>VACUUM</command> to recover the wasted space.
544 <literal>FORCE_NULL</literal> and <literal>FORCE_NOT_NULL</literal> can be used
545 simultaneously on the same column. This results in converting quoted
546 null strings to null values and unquoted null strings to empty strings.
552 <title>File Formats</title>
555 <title>Text Format</title>
558 When the <literal>text</literal> format is used,
559 the data read or written is a text file with one line per table row.
560 Columns in a row are separated by the delimiter character.
561 The column values themselves are strings generated by the
562 output function, or acceptable to the input function, of each
563 attribute's data type. The specified null string is used in
564 place of columns that are null.
565 <command>COPY FROM</command> will raise an error if any line of the
566 input file contains more or fewer columns than are expected.
570 End of data can be represented by a single line containing just
571 backslash-period (<literal>\.</literal>). An end-of-data marker is
572 not necessary when reading from a file, since the end of file
573 serves perfectly well; it is needed only when copying data to or from
574 client applications using pre-3.0 client protocol.
578 Backslash characters (<literal>\</literal>) can be used in the
579 <command>COPY</command> data to quote data characters that might
580 otherwise be taken as row or column delimiters. In particular, the
581 following characters <emphasis>must</emphasis> be preceded by a backslash if
582 they appear as part of a column value: backslash itself,
583 newline, carriage return, and the current delimiter character.
587 The specified null string is sent by <command>COPY TO</command> without
588 adding any backslashes; conversely, <command>COPY FROM</command> matches
589 the input against the null string before removing backslashes. Therefore,
590 a null string such as <literal>\N</literal> cannot be confused with
591 the actual data value <literal>\N</literal> (which would be represented
592 as <literal>\\N</literal>).
596 The following special backslash sequences are recognized by
597 <command>COPY FROM</command>:
603 <entry>Sequence</entry>
604 <entry>Represents</entry>
610 <entry><literal>\b</literal></entry>
611 <entry>Backspace (ASCII 8)</entry>
614 <entry><literal>\f</literal></entry>
615 <entry>Form feed (ASCII 12)</entry>
618 <entry><literal>\n</literal></entry>
619 <entry>Newline (ASCII 10)</entry>
622 <entry><literal>\r</literal></entry>
623 <entry>Carriage return (ASCII 13)</entry>
626 <entry><literal>\t</literal></entry>
627 <entry>Tab (ASCII 9)</entry>
630 <entry><literal>\v</literal></entry>
631 <entry>Vertical tab (ASCII 11)</entry>
634 <entry><literal>\</literal><replaceable>digits</replaceable></entry>
635 <entry>Backslash followed by one to three octal digits specifies
636 the character with that numeric code</entry>
639 <entry><literal>\x</literal><replaceable>digits</replaceable></entry>
640 <entry>Backslash <literal>x</literal> followed by one or two hex digits specifies
641 the character with that numeric code</entry>
647 Presently, <command>COPY TO</command> will never emit an octal or
648 hex-digits backslash sequence, but it does use the other sequences
649 listed above for those control characters.
653 Any other backslashed character that is not mentioned in the above table
654 will be taken to represent itself. However, beware of adding backslashes
655 unnecessarily, since that might accidentally produce a string matching the
656 end-of-data marker (<literal>\.</literal>) or the null string (<literal>\N</literal> by
657 default). These strings will be recognized before any other backslash
662 It is strongly recommended that applications generating <command>COPY</command> data convert
663 data newlines and carriage returns to the <literal>\n</literal> and
664 <literal>\r</literal> sequences respectively. At present it is
665 possible to represent a data carriage return by a backslash and carriage
666 return, and to represent a data newline by a backslash and newline.
667 However, these representations might not be accepted in future releases.
668 They are also highly vulnerable to corruption if the <command>COPY</command> file is
669 transferred across different machines (for example, from Unix to Windows
674 <command>COPY TO</command> will terminate each row with a Unix-style
675 newline (<quote><literal>\n</literal></quote>). Servers running on Microsoft Windows instead
676 output carriage return/newline (<quote><literal>\r\n</literal></quote>), but only for
677 <command>COPY</command> to a server file; for consistency across platforms,
678 <command>COPY TO STDOUT</command> always sends <quote><literal>\n</literal></quote>
679 regardless of server platform.
680 <command>COPY FROM</command> can handle lines ending with newlines,
681 carriage returns, or carriage return/newlines. To reduce the risk of
682 error due to un-backslashed newlines or carriage returns that were
683 meant as data, <command>COPY FROM</command> will complain if the line
684 endings in the input are not all alike.
689 <title>CSV Format</title>
692 This format option is used for importing and exporting the Comma
693 Separated Value (<literal>CSV</literal>) file format used by many other
694 programs, such as spreadsheets. Instead of the escaping rules used by
695 <productname>PostgreSQL</productname>'s standard text format, it
696 produces and recognizes the common CSV escaping mechanism.
700 The values in each record are separated by the <literal>DELIMITER</literal>
701 character. If the value contains the delimiter character, the
702 <literal>QUOTE</literal> character, the <literal>NULL</literal> string, a carriage
703 return, or line feed character, then the whole value is prefixed and
704 suffixed by the <literal>QUOTE</literal> character, and any occurrence
705 within the value of a <literal>QUOTE</literal> character or the
706 <literal>ESCAPE</literal> character is preceded by the escape character.
707 You can also use <literal>FORCE_QUOTE</literal> to force quotes when outputting
708 non-<literal>NULL</literal> values in specific columns.
712 The <literal>CSV</literal> format has no standard way to distinguish a
713 <literal>NULL</literal> value from an empty string.
714 <productname>PostgreSQL</productname>'s <command>COPY</command> handles this by quoting.
715 A <literal>NULL</literal> is output as the <literal>NULL</literal> parameter string
716 and is not quoted, while a non-<literal>NULL</literal> value matching the
717 <literal>NULL</literal> parameter string is quoted. For example, with the
718 default settings, a <literal>NULL</literal> is written as an unquoted empty
719 string, while an empty string data value is written with double quotes
720 (<literal>""</literal>). Reading values follows similar rules. You can
721 use <literal>FORCE_NOT_NULL</literal> to prevent <literal>NULL</literal> input
722 comparisons for specific columns. You can also use
723 <literal>FORCE_NULL</literal> to convert quoted null string data values to
724 <literal>NULL</literal>.
728 Because backslash is not a special character in the <literal>CSV</literal>
729 format, <literal>\.</literal>, the end-of-data marker, could also appear
730 as a data value. To avoid any misinterpretation, a <literal>\.</literal>
731 data value appearing as a lone entry on a line is automatically
732 quoted on output, and on input, if quoted, is not interpreted as the
733 end-of-data marker. If you are loading a file created by another
734 application that has a single unquoted column and might have a
735 value of <literal>\.</literal>, you might need to quote that value in the
741 In <literal>CSV</literal> format, all characters are significant. A quoted value
742 surrounded by white space, or any characters other than
743 <literal>DELIMITER</literal>, will include those characters. This can cause
744 errors if you import data from a system that pads <literal>CSV</literal>
745 lines with white space out to some fixed width. If such a situation
746 arises you might need to preprocess the <literal>CSV</literal> file to remove
747 the trailing white space, before importing the data into
748 <productname>PostgreSQL</productname>.
754 CSV format will both recognize and produce CSV files with quoted
755 values containing embedded carriage returns and line feeds. Thus
756 the files are not strictly one line per table row like text-format
763 Many programs produce strange and occasionally perverse CSV files,
764 so the file format is more a convention than a standard. Thus you
765 might encounter some files that cannot be imported using this
766 mechanism, and <command>COPY</command> might produce files that other
767 programs cannot process.
774 <title>Binary Format</title>
777 The <literal>binary</literal> format option causes all data to be
778 stored/read as binary format rather than as text. It is
779 somewhat faster than the text and <literal>CSV</literal> formats,
780 but a binary-format file is less portable across machine architectures and
781 <productname>PostgreSQL</productname> versions.
782 Also, the binary format is very data type specific; for example
783 it will not work to output binary data from a <type>smallint</type> column
784 and read it into an <type>integer</type> column, even though that would work
789 The <literal>binary</literal> file format consists
790 of a file header, zero or more tuples containing the row data, and
791 a file trailer. Headers and data are in network byte order.
796 <productname>PostgreSQL</productname> releases before 7.4 used a
797 different binary file format.
802 <title>File Header</title>
805 The file header consists of 15 bytes of fixed fields, followed
806 by a variable-length header extension area. The fixed fields are:
810 <term>Signature</term>
813 11-byte sequence <literal>PGCOPY\n\377\r\n\0</literal> — note that the zero byte
814 is a required part of the signature. (The signature is designed to allow
815 easy identification of files that have been munged by a non-8-bit-clean
816 transfer. This signature will be changed by end-of-line-translation
817 filters, dropped zero bytes, dropped high bits, or parity changes.)
823 <term>Flags field</term>
826 32-bit integer bit mask to denote important aspects of the file format. Bits
827 are numbered from 0 (<acronym>LSB</acronym>) to 31 (<acronym>MSB</acronym>). Note that
828 this field is stored in network byte order (most significant byte first),
829 as are all the integer fields used in the file format. Bits
830 16-31 are reserved to denote critical file format issues; a reader
831 should abort if it finds an unexpected bit set in this range. Bits 0-15
832 are reserved to signal backwards-compatible format issues; a reader
833 should simply ignore any unexpected bits set in this range. Currently
834 only one flag bit is defined, and the rest must be zero:
840 If 1, OIDs are included in the data; if 0, not. Oid system columns
841 are not supported in <productname>PostgreSQL</productname>
842 anymore, but the format still contains the indicator.
846 </variablelist></para>
851 <term>Header extension area length</term>
854 32-bit integer, length in bytes of remainder of header, not including self.
855 Currently, this is zero, and the first tuple follows
856 immediately. Future changes to the format might allow additional data
857 to be present in the header. A reader should silently skip over any header
858 extension data it does not know what to do with.
866 The header extension area is envisioned to contain a sequence of
867 self-identifying chunks. The flags field is not intended to tell readers
868 what is in the extension area. Specific design of header extension contents
869 is left for a later release.
873 This design allows for both backwards-compatible header additions (add
874 header extension chunks, or set low-order flag bits) and
875 non-backwards-compatible changes (set high-order flag bits to signal such
876 changes, and add supporting data to the extension area if needed).
881 <title>Tuples</title>
883 Each tuple begins with a 16-bit integer count of the number of fields in the
884 tuple. (Presently, all tuples in a table will have the same count, but that
885 might not always be true.) Then, repeated for each field in the tuple, there
886 is a 32-bit length word followed by that many bytes of field data. (The
887 length word does not include itself, and can be zero.) As a special case,
888 -1 indicates a NULL field value. No value bytes follow in the NULL case.
892 There is no alignment padding or any other extra data between fields.
896 Presently, all data values in a binary-format file are
897 assumed to be in binary format (format code one). It is anticipated that a
898 future extension might add a header field that allows per-column format codes
903 To determine the appropriate binary format for the actual tuple data you
904 should consult the <productname>PostgreSQL</productname> source, in
905 particular the <function>*send</function> and <function>*recv</function> functions for
906 each column's data type (typically these functions are found in the
907 <filename>src/backend/utils/adt/</filename> directory of the source
912 If OIDs are included in the file, the OID field immediately follows the
913 field-count word. It is a normal field except that it's not included in the
914 field-count. Note that oid system columns are not supported in current
915 versions of <productname>PostgreSQL</productname>.
920 <title>File Trailer</title>
923 The file trailer consists of a 16-bit integer word containing -1. This
924 is easily distinguished from a tuple's field-count word.
928 A reader should report an error if a field-count word is neither -1
929 nor the expected number of columns. This provides an extra
930 check against somehow getting out of sync with the data.
937 <title>Examples</title>
940 The following example copies a table to the client
941 using the vertical bar (<literal>|</literal>) as the field delimiter:
943 COPY country TO STDOUT (DELIMITER '|');
948 To copy data from a file into the <literal>country</literal> table:
950 COPY country FROM '/usr1/proj/bray/sql/country_data';
955 To copy into a file just the countries whose names start with 'A':
957 COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';
962 To copy into a compressed file, you can pipe the output through an external
965 COPY country TO PROGRAM 'gzip > /usr1/proj/bray/sql/country_data.gz';
970 Here is a sample of data suitable for copying into a table from
971 <literal>STDIN</literal>:
979 Note that the white space on each line is actually a tab character.
983 The following is the same data, output in binary format.
984 The data is shown after filtering through the
985 Unix utility <command>od -c</command>. The table has three columns;
986 the first has type <type>char(2)</type>, the second has type <type>text</type>,
987 and the third has type <type>integer</type>. All the rows have a null value
990 0000000 P G C O P Y \n 377 \r \n \0 \0 \0 \0 \0 \0
991 0000020 \0 \0 \0 \0 003 \0 \0 \0 002 A F \0 \0 \0 013 A
992 0000040 F G H A N I S T A N 377 377 377 377 \0 003
993 0000060 \0 \0 \0 002 A L \0 \0 \0 007 A L B A N I
994 0000100 A 377 377 377 377 \0 003 \0 \0 \0 002 D Z \0 \0 \0
995 0000120 007 A L G E R I A 377 377 377 377 \0 003 \0 \0
996 0000140 \0 002 Z M \0 \0 \0 006 Z A M B I A 377 377
997 0000160 377 377 \0 003 \0 \0 \0 002 Z W \0 \0 \0 \b Z I
998 0000200 M B A B W E 377 377 377 377 377 377
999 </programlisting></para>
1003 <title>Compatibility</title>
1006 There is no <command>COPY</command> statement in the SQL standard.
1010 The following syntax was used before <productname>PostgreSQL</productname>
1011 version 9.0 and is still supported:
1014 COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
1015 FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
1018 [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter_character</replaceable>' ]
1019 [ NULL [ AS ] '<replaceable class="parameter">null_string</replaceable>' ]
1021 [ QUOTE [ AS ] '<replaceable class="parameter">quote_character</replaceable>' ]
1022 [ ESCAPE [ AS ] '<replaceable class="parameter">escape_character</replaceable>' ]
1023 [ FORCE NOT NULL <replaceable class="parameter">column_name</replaceable> [, ...] ] ] ]
1025 COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
1026 TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
1029 [ DELIMITER [ AS ] '<replaceable class="parameter">delimiter_character</replaceable>' ]
1030 [ NULL [ AS ] '<replaceable class="parameter">null_string</replaceable>' ]
1032 [ QUOTE [ AS ] '<replaceable class="parameter">quote_character</replaceable>' ]
1033 [ ESCAPE [ AS ] '<replaceable class="parameter">escape_character</replaceable>' ]
1034 [ FORCE QUOTE { <replaceable class="parameter">column_name</replaceable> [, ...] | * } ] ] ]
1037 Note that in this syntax, <literal>BINARY</literal> and <literal>CSV</literal> are
1038 treated as independent keywords, not as arguments of a <literal>FORMAT</literal>
1043 The following syntax was used before <productname>PostgreSQL</productname>
1044 version 7.3 and is still supported:
1047 COPY [ BINARY ] <replaceable class="parameter">table_name</replaceable>
1048 FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
1049 [ [USING] DELIMITERS '<replaceable class="parameter">delimiter_character</replaceable>' ]
1050 [ WITH NULL AS '<replaceable class="parameter">null_string</replaceable>' ]
1052 COPY [ BINARY ] <replaceable class="parameter">table_name</replaceable>
1053 TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
1054 [ [USING] DELIMITERS '<replaceable class="parameter">delimiter_character</replaceable>' ]
1055 [ WITH NULL AS '<replaceable class="parameter">null_string</replaceable>' ]