1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
16 HREF="sql-commands.html"><LINK
19 HREF="sql-commit.html"><LINK
21 TITLE="CREATE AGGREGATE"
22 HREF="sql-createaggregate.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
51 HREF="sql-commit.html"
60 HREF="sql-commit.html"
73 HREF="sql-createaggregate.html"
81 HREF="sql-createaggregate.html"
102 >COPY -- copy data between a file and a table</DIV
107 CLASS="REFSYNOPSISDIV"
129 [ DELIMITER [ AS ] '<VAR
152 [ DELIMITER [ AS ] '<VAR
176 > tables and standard file-system
180 > copies the contents of a table
197 > a file to a table (appending the data to
198 whatever is in the table already).
201 > If a list of columns is specified, <TT
205 only copy the data in the specified columns to or from the file.
206 If there are any columns in the table that are not in the column list,
210 > will insert the default values for
217 > with a file name instructs the
221 > server to directly read from
222 or write to a file. The file must be accessible to the server and
223 the name must be specified from the viewpoint of the server. When
231 specified, data is transmitted via the connection between the
232 client and the server.
254 > The name (optionally schema-qualified) of an existing table.
264 > An optional list of columns to be copied. If no column list is
265 specified, all columns will be used.
275 > The absolute path name of the input or output file.
285 > Specifies that input comes from the client application.
295 > Specifies that output goes to the client application.
305 > Causes all data to be stored or read in binary format rather
306 than as text. You cannot specify the <VAR
313 > options in binary mode.
323 > Specifies copying the OID for each row. (An error is raised if
327 > is specified for a table that does not
338 > The single character that separates columns within each row
339 (line) of the file. The default is a tab character.
349 > The string that represents a null value. The default is
353 > (backslash-N). You might prefer an empty
366 >, any data item that matches
367 this string will be stored as a null value, so you should make
368 sure that you use the same string as you used with
391 > can only be used with plain tables, not
398 > key word causes all data to be
399 stored/read as binary format rather than as text. It is
400 somewhat faster than the normal text mode, but a binary-format
401 file is less portable across machine architectures and
408 > You must have select privilege on the table
409 whose values are read by <TT
413 insert privilege on the table into which values
420 > Files named in a <TT
423 > command are read or written
424 directly by the server, not by the client application. Therefore,
425 they must reside on or be accessible to the database server machine,
426 not the client. They must be accessible to and readable or writable
430 > user (the user ID the
431 server runs as), not the client. <TT
435 file is only allowed to database superusers, since it allows reading
436 or writing any file that the server has privileges to access.
461 >, and then fetches/stores the data in a file
462 accessible to the <SPAN
466 file accessibility and access rights depend on the client rather
467 than the server when <TT
473 > It is recommended that the file name used in <TT
477 always be specified as an absolute path. This is enforced by the
478 server in the case of <TT
485 > you do have the option of reading from
486 a file specified by a relative path. The path will be interpreted
487 relative to the working directory of the server process (somewhere below
488 the data directory), not the client's working directory.
494 > will invoke any triggers and check
495 constraints on the destination table. However, it will not invoke rules.
501 > stops operation at the first error. This
502 should not lead to problems in the event of a <TT
506 >, but the target table will already have received
507 earlier rows in a <TT
510 >. These rows will not
511 be visible or accessible, but they still occupy disk space. This may
512 amount to a considerable amount of wasted disk space if the failure
513 happened well into a large copy operation. You may wish to invoke
517 > to recover the wasted space.
538 > is used without the <TT
542 the data read or written is a text file with one line per table row.
543 Columns in a row are separated by the delimiter character.
544 The column values themselves are strings generated by the
545 output function, or acceptable to the input function, of each
546 attribute's data type. The specified null string is used in
547 place of columns that are null.
551 > will raise an error if any line of the
552 input file contains more or fewer columns than are expected.
556 > is specified, the OID is read or written as the first column,
557 preceding the user data columns.
560 > End of data can be represented by a single line containing just
561 backslash-period (<TT
564 >). An end-of-data marker is
565 not necessary when reading from a file, since the end of file
566 serves perfectly well; it is needed only when copying data to or from
567 client applications using pre-3.0 client protocol.
570 > Backslash characters (<TT
573 >) may be used in the
577 > data to quote data characters that might
578 otherwise be taken as row or column delimiters. In particular, the
579 following characters <SPAN
585 > be preceded by a backslash if
586 they appear as part of a column value: backslash itself,
587 newline, carriage return, and the current delimiter character.
590 > The specified null string is sent by <TT
594 adding any backslashes; conversely, <TT
598 the input against the null string before removing backslashes. Therefore,
599 a null string such as <TT
602 > cannot be confused with
603 the actual data value <TT
606 > (which would be represented
613 > The following special backslash sequences are recognized by
620 CLASS="INFORMALTABLE"
645 >Backspace (ASCII 8)</TD
654 >Form feed (ASCII 12)</TD
663 >Newline (ASCII 10)</TD
672 >Carriage return (ASCII 13)</TD
690 >Vertical tab (ASCII 11)</TD
702 >Backslash followed by one to three octal digits specifies
703 the character with that numeric code</TD
715 > will never emit an octal-digits
716 backslash sequence, but it does use the other sequences listed above
717 for those control characters.
720 > Any other backslashed character that is not mentioned in the above table
721 will be taken to represent itself. However, beware of adding backslashes
722 unnecessarily, since that might accidentally produce a string matching the
723 end-of-data marker (<TT
726 >) or the null string (<TT
730 default). These strings will be recognized before any other backslash
734 > It is strongly recommended that applications generating COPY data convert
735 data newlines and carriage returns to the <TT
742 > sequences respectively. At present it is
743 possible to represent a data carriage return by a backslash and carriage
744 return, and to represent a data newline by a backslash and newline.
745 However, these representations might not be accepted in future releases.
746 They are also highly vulnerable to corruption if the COPY file is
747 transferred across different machines (for example, from Unix to Windows
754 > will terminate each row with a Unix-style
761 >). Servers running on MS Windows instead
762 output carriage return/newline (<SPAN
772 > to a server file; for consistency across platforms,
783 regardless of server platform.
787 > can handle lines ending with newlines,
788 carriage returns, or carriage return/newlines. To reduce the risk of
789 error due to un-backslashed newlines or carriage returns that were
793 > will complain if the line
794 endings in the input are not all alike.
805 > The file format used for <TT
812 > 7.4. The new format consists
813 of a file header, zero or more tuples containing the row data, and
814 a file trailer. Headers and data are now in network byte order.
824 > The file header consists of 15 bytes of fixed fields, followed
825 by a variable-length header extension area. The fixed fields are:
836 >11-byte sequence <TT
838 >PGCOPY\n\377\r\n\0</TT
839 > --- note that the zero byte
840 is a required part of the signature. (The signature is designed to allow
841 easy identification of files that have been munged by a non-8-bit-clean
842 transfer. This signature will be changed by end-of-line-translation
843 filters, dropped zero bytes, dropped high bits, or parity changes.)
850 >32-bit integer bit mask to denote important aspects of the file format. Bits
851 are numbered from 0 (<ACRONYM
858 this field is stored in network byte order (most significant byte first),
859 as are all the integer fields used in the file format. Bits
860 16-31 are reserved to denote critical file format issues; a reader
861 should abort if it finds an unexpected bit set in this range. Bits 0-15
862 are reserved to signal backwards-compatible format issues; a reader
863 should simply ignore any unexpected bits set in this range. Currently
864 only one flag bit is defined, and the rest must be zero:
874 > if 1, OIDs are included in the data; if 0, not
883 >Header extension area length</DT
886 >32-bit integer, length in bytes of remainder of header, not including self.
887 Currently, this is zero, and the first tuple follows
888 immediately. Future changes to the format might allow additional data
889 to be present in the header. A reader should silently skip over any header
890 extension data it does not know what to do with.
898 >The header extension area is envisioned to contain a sequence of
899 self-identifying chunks. The flags field is not intended to tell readers
900 what is in the extension area. Specific design of header extension contents
901 is left for a later release.
904 > This design allows for both backwards-compatible header additions (add
905 header extension chunks, or set low-order flag bits) and
906 non-backwards-compatible changes (set high-order flag bits to signal such
907 changes, and add supporting data to the extension area if needed).
918 >Each tuple begins with a 16-bit integer count of the number of fields in the
919 tuple. (Presently, all tuples in a table will have the same count, but that
920 might not always be true.) Then, repeated for each field in the tuple, there
921 is a 32-bit length word followed by that many bytes of field data. (The
922 length word does not include itself, and can be zero.) As a special case,
923 -1 indicates a NULL field value. No value bytes follow in the NULL case.
926 >There is no alignment padding or any other extra data between fields.
929 >Presently, all data values in a <TT
933 assumed to be in binary format (format code one). It is anticipated that a
934 future extension may add a header field that allows per-column format codes
938 >To determine the appropriate binary format for the actual tuple data you
939 should consult the <SPAN
950 each column's data type (typically these functions are found in the
953 >src/backend/utils/adt/</TT
954 > directory of the source
958 >If OIDs are included in the file, the OID field immediately follows the
959 field-count word. It is a normal field except that it's not included
960 in the field-count. In particular it has a length word --- this will allow
961 handling of 4-byte vs. 8-byte OIDs without too much pain, and will allow
962 OIDs to be shown as null if that ever proves desirable.
973 > The file trailer consists of a 16-bit integer word containing -1. This
974 is easily distinguished from a tuple's field-count word.
977 > A reader should report an error if a field-count word is neither -1
978 nor the expected number of columns. This provides an extra
979 check against somehow getting out of sync with the data.
992 > The following example copies a table to the client
993 using the vertical bar (<TT
996 >) as the field delimiter:
998 CLASS="PROGRAMLISTING"
999 >COPY country TO STDOUT WITH DELIMITER '|';</PRE
1003 > To copy data from a file into the <TT
1008 CLASS="PROGRAMLISTING"
1009 >COPY country FROM '/usr1/proj/bray/sql/country_data';</PRE
1013 > Here is a sample of data suitable for copying into a table from
1019 CLASS="PROGRAMLISTING"
1026 Note that the white space on each line is actually a tab character.
1029 > The following is the same data, output in binary format.
1030 The data is shown after filtering through the
1034 >. The table has three columns;
1035 the first has type <TT
1038 >, the second has type <TT
1042 and the third has type <TT
1045 >. All the rows have a null value
1046 in the third column.
1048 CLASS="PROGRAMLISTING"
1049 >0000000 P G C O P Y \n 377 \r \n \0 \0 \0 \0 \0 \0
1050 0000020 \0 \0 \0 \0 003 \0 \0 \0 002 A F \0 \0 \0 013 A
1051 0000040 F G H A N I S T A N 377 377 377 377 \0 003
1052 0000060 \0 \0 \0 002 A L \0 \0 \0 007 A L B A N I
1053 0000100 A 377 377 377 377 \0 003 \0 \0 \0 002 D Z \0 \0 \0
1054 0000120 007 A L G E R I A 377 377 377 377 \0 003 \0 \0
1055 0000140 \0 002 Z M \0 \0 \0 006 Z A M B I A 377 377
1056 0000160 377 377 \0 003 \0 \0 \0 002 Z W \0 \0 \0 \b Z I
1057 0000200 M B A B W E 377 377 377 377 377 377</PRE
1072 > statement in the SQL standard.
1075 > The following syntax was used before PostgreSQL version 7.3 and is
1080 >COPY [ BINARY ] <VAR
1088 [ [USING] DELIMITERS '<VAR
1092 [ WITH NULL AS '<VAR
1097 COPY [ BINARY ] <VAR
1105 [ [USING] DELIMITERS '<VAR
1109 [ WITH NULL AS '<VAR
1121 SUMMARY="Footer navigation table"
1132 HREF="sql-commit.html"
1150 HREF="sql-createaggregate.html"
1166 HREF="sql-commands.html"
1174 >CREATE AGGREGATE</TD