@ -14,69 +14,112 @@
</REFPURPOSE>
</REFPURPOSE>
<REFSYNOPSISDIV>
<REFSYNOPSISDIV>
<REFSYNOPSISDIVINFO>
<REFSYNOPSISDIVINFO>
<DATE>1998-04-15 </DATE>
<DATE>1998-09-08 </DATE>
</REFSYNOPSISDIVINFO>
</REFSYNOPSISDIVINFO>
<SYNOPSIS>
<SYNOPSIS>
COPY [BINARY] <replaceable class="parameter">table</replaceable> [WITH OIDS]
COPY [ BINARY ] <replaceable class="parameter">table</replaceable> [ WITH OIDS ]
TO|FROM '<replaceable class="parameter">filename</replaceable>'|stdin|stdout
FROM { '<replaceable class="parameter">filename</replaceable>' | <filename>stdin</filename> }
[USING DELIMITERS '<replaceable class="parameter">delimiter</replaceable>']
[ USING DELIMITERS '<replaceable class="parameter">delimiter</replaceable>' ]
COPY [ BINARY ] <replaceable class="parameter">table</replaceable> [ WITH OIDS ]
TO { '<replaceable class="parameter">filename</replaceable>' | <filename>stdout</filename> }
[ USING DELIMITERS '<replaceable class="parameter">delimiter</replaceable>' ]
</SYNOPSIS>
</SYNOPSIS>
<REFSECT2 ID="R2-SQL-COPY-1">
<REFSECT2 ID="R2-SQL-COPY-1">
<REFSECT2INFO>
<REFSECT2INFO>
<DATE>1998-04-15 </DATE>
<DATE>1998-09-08 </DATE>
</REFSECT2INFO>
</REFSECT2INFO>
<TITLE>
<TITLE>
Inputs
Inputs
</TITLE>
</TITLE>
<PARA>
<PARA>
</PARA>
<VARIABLELIST>
<VARIABLELIST>
<VARLISTENTRY>
<VARLISTENTRY>
<TERM>
<TERM>
BINARY
</TERM>
</TERM>
<LISTITEM>
<LISTITEM>
<PARA>
<PARA>
<VARIABLELIST>
Changes the behavior of field formatting, forcing all data to be
stored or read as binary objects rather than as text.
</PARA>
</LISTITEM>
</VARLISTENTRY>
<VARLISTENTRY>
<VARLISTENTRY>
<TERM>
<TERM>
<ReturnValue><replaceable class="parameter">table</replaceable></ReturnValue>
<replaceable class="parameter">table</replaceable>
</TERM>
</TERM>
<LISTITEM>
<LISTITEM>
<PARA>
<PARA>
The name of a table.
The name of an existing table.
</PARA>
</PARA>
</LISTITEM>
</LISTITEM>
</VARLISTENTRY>
</VARLISTENTRY>
<VARLISTENTRY>
<VARLISTENTRY>
<TERM>
<TERM>
<ReturnValue><replaceable class="parameter">delimiter</replaceable></ReturnValue>
WITH OIDS
</TERM>
</TERM>
<LISTITEM>
<LISTITEM>
<PARA>
<PARA>
A character that delimits fields .
Copies the internal unique object id (OID) for each row .
</PARA>
</PARA>
</LISTITEM>
</LISTITEM>
</VARLISTENTRY>
</VARLISTENTRY>
</variablelist>
<VARLISTENTRY>
<TERM>
<replaceable class="parameter">filename</replaceable>
</TERM>
<LISTITEM>
<PARA>
The absolute Unix pathname of the input or output file.
</PARA>
</LISTITEM>
</LISTITEM>
</VARLISTENTRY>
</VARLISTENTRY>
</VARIABLELIST>
<VARLISTENTRY>
<TERM>
<filename>stdin</filename>
</TERM>
<LISTITEM>
<PARA>
Specifies that input comes from a pipe or terminal.
</PARA>
</LISTITEM>
</VARLISTENTRY>
<VARLISTENTRY>
<TERM>
<filename>stdout</filename>
</TERM>
<LISTITEM>
<PARA>
Specifies that output goes to a pipe or terminal.
</PARA>
</LISTITEM>
</VARLISTENTRY>
<VARLISTENTRY>
<TERM>
<replaceable class="parameter">delimiter</replaceable>
</TERM>
<LISTITEM>
<PARA>
A character that delimits the input or output fields.
</PARA>
</LISTITEM>
</VARLISTENTRY>
</variablelist>
</REFSECT2>
</REFSECT2>
<REFSECT2 ID="R2-SQL-COPY-2">
<REFSECT2 ID="R2-SQL-COPY-2">
<REFSECT2INFO>
<REFSECT2INFO>
<DATE>1998-04-15</DATE>
<DATE>1998-09-08 </DATE>
</REFSECT2INFO>
</REFSECT2INFO>
<TITLE>
<TITLE>
Outputs
Outputs
</TITLE>
</TITLE>
<PARA>
<PARA>
</PARA>
<VARIABLELIST>
<VARIABLELIST>
<VARLISTENTRY>
<VARLISTENTRY>
<TERM>
<TERM>
Status
<Replaceable>status</Replaceable>
</TERM>
</TERM>
<LISTITEM>
<LISTITEM>
<PARA>
<PARA>
@ -110,84 +153,82 @@
<REFSECT1 ID="R1-SQL-COPY-1">
<REFSECT1 ID="R1-SQL-COPY-1">
<REFSECT1INFO>
<REFSECT1INFO>
<DATE>1998-04-15 </DATE>
<DATE>1998-09-08 </DATE>
</REFSECT1INFO>
</REFSECT1INFO>
<TITLE>
<TITLE>
Description
Description
</TITLE>
</TITLE>
<PARA>
<PARA>
<command>COPY</command> moves data between PostgreSQL tables and
<command>COPY</command> moves data between
standard Unix files. The keyword <function>BINARY</function>
<productname>Postgres</productname> tables and
changes the behavior of field formatting, as described
standard Unix files.
below. <replaceable class="parameter">Table</replaceable> is the
name of an existing table. The keyword <function>WITH
<para>
OIDS</function> copies the internal unique object id (OID) for each
<command>COPY</command> instructs
row. <replaceable class="parameter">Filename</replaceable> is the
the <productname>Postgres</productname> backend
absolute Unix pathname of the file. In place of a filename, the
to directly read from or write to a file. The file must be directly visible to
keywords <function>stdin</function> and <function>stdout</function>
the backend and the name must be specified from the viewpoint of the backend.
can be used, so that input to <command>COPY</command> can be written
If <filename>stdin</filename> or <filename>stdout</filename> are specified, data flows through the client frontend to
by a libpq application and output from <command>COPY</command> can
the backend.
be read by a libpq application.
</para>
<REFSECT2 ID="R2-SQL-COPY-3">
<REFSECT2INFO>
<DATE>1998-09-08</DATE>
</REFSECT2INFO>
<TITLE>
Notes
</TITLE>
<para>
<para>
The <function>BINARY</function> keyword will force all data to be
The BINARY keyword will force all data to be
stored/read as binary objects rather than as ASCII text. It is
stored/read as binary objects rather than as text. It is
somewhat faster than the normal copy command, but is not
somewhat faster than the normal copy command, but is not
generally portable, and the files generated are somewhat larger,
generally portable, and the files generated are somewhat larger,
although this factor is highly dependent on the data itself. By
although this factor is highly dependent on the data itself. By
default, an ASCII copy uses a tab (\t ) character as a delimiter.
default, a text copy uses a tab ("\t" ) character as a delimiter.
The delimiter may also be changed to any other single character
The delimiter may also be changed to any other single character
with the keyword <function>USING DELIMITERS</function> . Characters
with the keyword phrase USING DELIMITERS . Characters
in data fields which happen to match the delimiter character will
in data fields which happen to match the delimiter character will
be quoted.
be quoted.
</para>
</para>
<REFSECT2 ID="R2-SQL-COPY-3">
<REFSECT2INFO>
<DATE>1998-04-15</DATE>
</REFSECT2INFO>
<TITLE>
Notes
</TITLE>
<para>
<para>
You must have select access on any table whose values are read by
You must have select access on any table whose values are read by
<command>COPY</command>, and either insert or update access to a
<command>COPY</command>, and either insert or update access to a
table into which values are being inserted by <command>COPY</command>.
table into which values are being inserted by <command>COPY</command>.
The backend also needs appropriate Unix permissions for any file read
The backend also needs appropriate Unix permissions for any file read
or written by <command>COPY</command>.
or written by <command>COPY</command>.
<comment>
Is this right? The man page talked of read, write and append access, which
is neither SQL nor Unix terminology.
</comment>
</para>
</para>
<para>
<para>
The keyword <function>USING DELIMITERS</function> is inaptly
The keyword phrase USING DELIMITERS specifies a single character
named, since only a single character may be specified. (If a
to be used for all delimiters between columns. If multiple characters
group of characters is specified, only the first character is
are specified in the delimiter string, only the first character is
used.)
used.
</para>
<para>
<tip>
WARNING: do not confuse <command>COPY</command> with the
<para>
<command>psql</command> instruction <command>\copy</command>.
Do not confuse <command>COPY</command> with the
</para>
<application>psql</application> instruction <command>\copy</command>.
</tip>
</REFSECT2>
</REFSECT2>
</refsect1>
</refsect1>
<refsect1 ID="R1-SQL-COPY-2">
<refsect1 ID="R1-SQL-COPY-2">
<refsect1info>
<refsect1info>
<date>1998-05-04</date>
<date>1998-05-04</date>
</refsect1info>
</refsect1info>
<title>Format of output file s</title>
<title>File F ormats</title>
<refsect2>
<refsect2>
<refsect2info>
<refsect2info>
<date>1998-05-04</date>
<date>1998-05-04</date>
</refsect2info>
</refsect2info>
<title>ASCII copy f ormat</title>
<title>Text F ormat</title>
<para>
<para>
When <command>COPY</command> is used without <function>BINARY</function> ,
When <command>COPY TO</command> is used without the BINARY option ,
the file generated will have each instance on a single line, with each
the file generated will have each row ( instance) on a single line, with each
attribute separated by the delimiter character. Embedded
column ( attribute) separated by the delimiter character. Embedded
delimiter characters will be preceded by a backslash character
delimiter characters will be preceded by a backslash character
(\). The attribute values themselves are strings generated by the
(" \" ). The attribute values themselves are strings generated by the
output function associated with each attribute type. The output
output function associated with each attribute type. The output
function for a type should not try to generate the backslash
function for a type should not try to generate the backslash
character; this will be handled by <command>COPY</command> itself.
character; this will be handled by <command>COPY</command> itself.
@ -195,29 +236,31 @@ is neither SQL nor Unix terminology.
<para>
<para>
The actual format for each instance is
The actual format for each instance is
<programlisting>
<programlisting>
<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>
<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>
The oid is placed on the beginning of the line
The oid is placed on the beginning of the line
if <function> WITH OIDS</function> is specified.
if WITH OIDS is specified.
</para>
</para>
<para>
<para>
If <command>COPY</command> is sending its output to standard
If <command>COPY</command> is sending its output to standard
output instead of a file, it will send a backslash(\) and a period
output instead of a file, it will send a backslash(" \" ) and a period
(.) followed immediately by a newline, on a separate line,
(" ." ) followed immediately by a newline, on a separate line,
when it is done. Similarly, if <command>COPY</command> is reading
when it is done. Similarly, if <command>COPY</command> is reading
from standard input, it will expect a backslash (\) and a period
from standard input, it will expect a backslash (" \" ) and a period
(.) followed by a newline, as the first three characters on a
(" ." ) followed by a newline, as the first three characters on a
line, to denote end-of-file. However, <command>COPY</command>
line to denote end-of-file. However, <command>COPY</command>
will terminate (followed by the backend itself) if a true EOF is
will terminate (followed by the backend itself) if a true EOF is
encountered.
encountered before this special end-of-file pattern is found .
</para>
</para>
<para>
<para>
The backslash character has special meaning. NULL attributes are
The backslash character has other special meanings . NULL attributes are
output as \N. A literal backslash character is output as two
output as " \N" . A literal backslash character is output as two
consecutive backslashes. A literal tab character is represented
consecutive backslashes ("\\") . A literal tab character is represented
as a backslash and a tab. A literal newline character is
as a backslash and a tab. A literal newline character is
represented as a backslash and a newline. When loading ASCII data
represented as a backslash and a newline. When loading text data
not generated by PostgreSQL, you will need to convert backslash
not generated by <acronym>Postgres</acronym>,
characters (\) to double-backslashes (\\) to ensure that they are loaded
you will need to convert backslash
characters ("\") to double-backslashes ("\\") to ensure that they are loaded
properly.
properly.
</para>
</para>
</refsect2>
</refsect2>
@ -225,7 +268,7 @@ is neither SQL nor Unix terminology.
<refsect2info>
<refsect2info>
<date>1998-05-04</date>
<date>1998-05-04</date>
</refsect2info>
</refsect2info>
<title>Binary copy f ormat</title>
<title>Binary F ormat</title>
<para>
<para>
In the case of <command>COPY BINARY</command>, the first four
In the case of <command>COPY BINARY</command>, the first four
bytes in the file will be the number of instances in the file. If
bytes in the file will be the number of instances in the file. If
@ -270,16 +313,8 @@ is neither SQL nor Unix terminology.
<entry>number of null attributes</entry>
<entry>number of null attributes</entry>
</row>
</row>
<row>
<row>
<entry>[uint32</entry>
<entry>[uint32,...,uint32]</entry>
<entry>attribute number of first null attribute, counting from 0</entry>
<entry>attribute numbers of attributes, counting from 0</entry>
</row>
<row>
<entry>...</entry>
<entry>...</entry>
</row>
<row>
<entry>uint32</entry>
<entry>attribute number of last null attribute]</entry>
</row>
</row>
<row>
<row>
<entry>-</entry>
<entry>-</entry>
@ -294,12 +329,12 @@ is neither SQL nor Unix terminology.
<refsect2info>
<refsect2info>
<date>1998-05-04</date>
<date>1998-05-04</date>
</refsect2info>
</refsect2info>
<title>Alignment of binary d ata</title>
<title>Alignment of Binary D ata</title>
<para>
<para>
On Sun-3s, 2-byte attributes are aligned on two-byte boundaries,
On Sun-3s, 2-byte attributes are aligned on two-byte boundaries,
and all larger attributes are aligned on four-byte boundaries.
and all larger attributes are aligned on four-byte boundaries.
Character attributes are aligned on single-byte boundaries. On
Character attributes are aligned on single-byte boundaries. On
other machines, all attributes larger than 1 byte are aligned on
most other machines, all attributes larger than 1 byte are aligned on
four-byte boundaries. Note that variable length attributes are
four-byte boundaries. Note that variable length attributes are
preceded by the attribute's length; arrays are simply contiguous
preceded by the attribute's length; arrays are simply contiguous
streams of the array element type.
streams of the array element type.
@ -313,19 +348,22 @@ is neither SQL nor Unix terminology.
Usage
Usage
</TITLE>
</TITLE>
<PARA>
<PARA>
To copy a table to standard output, using | as a delimiter
The following example copies a table to standard output,
using a vertical bar ("|") as the field
delimiter:
</PARA>
</PARA>
<ProgramListing>
<ProgramListing>
COPY country TO stdout USING DELIMITERS '|';
COPY country TO <filename> stdout</filename> USING DELIMITERS '|';
</ProgramListing>
</ProgramListing>
<PARA>
<PARA>
To copy data from a Unix file into a table:
To copy data from a Unix file into a table "country" :
</PARA>
</PARA>
<ProgramListing>
<ProgramListing>
COPY country FROM '/usr1/proj/bray/sql/country_data';
COPY country FROM '/usr1/proj/bray/sql/country_data';
</ProgramListing>
</ProgramListing>
<PARA>
<PARA>
A sample of data suitable for copying into a table from <filename>stdin</filename> (so it
Here is a sample of data suitable for copying into a table
from <filename>stdin</filename> (so it
has the termination sequence on the last line):
has the termination sequence on the last line):
</PARA>
</PARA>
<ProgramListing>
<ProgramListing>
@ -338,10 +376,13 @@ has the termination sequence on the last line):
\.
\.
</ProgramListing>
</ProgramListing>
<PARA>
<PARA>
The same data, output in binary format on a Linux Intel machine.
The same data, output in binary format on a Linux/i586 machine.
The data is shown after filtering through the Unix utility <command>od -c</command>. The table has
The data is shown after filtering through
three fields; the first is <classname>char(2)</classname> and the second is <classname>text</classname>. All the
the Unix utility <command>od -c</command>. The table has
rows have a null value in the third field). Notice how the <classname>char(2)</classname>
three fields; the first is <classname>char(2)</classname>
and the second is <classname>text</classname>. All the
rows have a null value in the third field.
Notice how the <classname>char(2)</classname>
field is padded with nulls to four bytes and the text field is
field is padded with nulls to four bytes and the text field is
preceded by its length:
preceded by its length:
</PARA>
</PARA>
@ -359,32 +400,32 @@ has the termination sequence on the last line):
</ProgramListing>
</ProgramListing>
</refsect1>
</refsect1>
<refsect1 ID="R1-SQL-COPY-4">
<title>See also</title>
<para>
insert(l), create table(l), vacuum(l), libpq.
</para>
</refsect1>
<refsect1 ID="R1-SQL-COPY-5">
<refsect1 ID="R1-SQL-COPY-5">
<title>Bugs</title>
<title>Bugs</title>
<para>
<para>
<command>COPY</command> stops operation at the first error. This
<command>COPY</command> stops operation at the first error. This
should not lead to problems in the event of a copy from, but the
should not lead to problems in the event of
target relation will, of course, be partially modified in a copy
a <command>COPY FROM</command>, but the
to. The <command>VACUUM</command> query should be used to clean up
target relation will, of course, be partially modified in a
<command>COPY TO</command>.
The <command>VACUUM</command> query should be used to clean up
after a failed copy.
after a failed copy.
</para>
</para>
<para>
<para>
Because Postgres' current directory is not the same as the user's
Because the Postgres backend's current working directory
working directory, the result of copying to a file "foo" (without
is not usually the same as the user's
working directory, the result of copying to a file
"<filename>foo</filename>" (without
additional path information) may yield unexpected results for the
additional path information) may yield unexpected results for the
naive user. In this case, "foo" will wind up in $PGDATA/foo. In
naive user. In this case, <filename>foo</filename>
general, the full pathname should be used when specifying files to
will wind up in <filename>$PGDATA/foo</filename>. In
general, the full pathname as it would appear to the backend server machine
should be used when specifying files to
be copied.
be copied.
</para>
</para>
<para>
<para>
Files used as arguments to the copy command must reside on or be
Files used as arguments to <command>COPY</command>
must reside on or be
accessible to the database server machine by being either on
accessible to the database server machine by being either on
local disks or on a networked file system.
local disks or on a networked file system.
</para>
</para>
@ -405,13 +446,13 @@ has the termination sequence on the last line):
<REFSECT2 ID="R2-SQL-COPY-4">
<REFSECT2 ID="R2-SQL-COPY-4">
<REFSECT2INFO>
<REFSECT2INFO>
<DATE>1998-04-15 </DATE>
<DATE>1998-09-08 </DATE>
</REFSECT2INFO>
</REFSECT2INFO>
<TITLE>
<TITLE>
SQL92
SQL92
</TITLE>
</TITLE>
<PARA>
<PARA>
There is no COPY statement in SQL92.
There is no <command> COPY</command> statement in SQL92.
</PARA>
</PARA>
</refsect2>
</refsect2>
</refsect1>
</refsect1>