Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/ref/copy.sgml`
b658b6713ba46e0f594355910ad12a72d2832972acf980800000000100000fa0
 <command>COPY TO</command>
   can also copy the results of a <command>SELECT</command> query.
  </para>

  <para>
   If a column list is specified, <command>COPY TO</command> copies only
   the data in the specified columns to the file.  For <command>COPY
   FROM</command>, each field in the file is inserted, in order, into the
   specified column.  Table columns not specified in the <command>COPY
   FROM</command> column list will receive their default values.
  </para>

  <para>
   <command>COPY</command> with a file name instructs the
   <productname>PostgreSQL</productname> server to directly read from
   or write to a file. The file must be accessible by the
   <productname>PostgreSQL</productname> user (the user ID the server
   runs as) and the name must be specified from the viewpoint of the
   server. When <literal>PROGRAM</literal> is specified, the server
   executes the given command and reads from the standard output of the
   program, or writes to the standard input of the program. The command
   must be specified from the viewpoint of the server, and be executable
   by the <productname>PostgreSQL</productname> user.  When
   <literal>STDIN</literal> or <literal>STDOUT</literal> is
   specified, data is transmitted via the connection between the
   client and the server.
  </para>

  <para>
    Each backend running <command>COPY</command> will report its progress
    in the <structname>pg_stat_progress_copy</structname> view. See
    <xref linkend="copy-progress-reporting"/> for details.
  </para>

  <para>
    By default, <command>COPY</command> will fail if it encounters an error
    during processing. For use cases where a best-effort attempt at loading
    the entire file is desired, the <literal>ON_ERROR</literal> clause can
    be used to specify some other behavior.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><replaceable class="parameter">table_name</replaceable></term>
    <listitem>
     <para>
      The name (optionally schema-qualified) of an existing table.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">column_name</replaceable></term>
     <listitem>
     <para>
      An optional list of columns to be copied.  If no column list is
      specified, all columns of the table except generated columns will be
      copied.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">query</replaceable></term>
    <listitem>
     <para>
      A <link linkend="sql-select"><command>SELECT</command></link>,
      <link linkend="sql-values"><command>VALUES</command></link>,
      <link linkend="sql-insert"><command>INSERT</command></link>,
      <link linkend="sql-update"><command>UPDATE</command></link>,
      <link linkend="sql-delete"><command>DELETE</command></link>, or
      <link linkend="sql-merge"><command>MERGE</command></link> command
      whose results are to be copied.  Note that parentheses are required
      around the query.
     </para>
     <para>
      For <command>INSERT</command>, <command>UPDATE</command>,
      <command>DELETE</command>, and <command>MERGE</command> queries a
      <literal>RETURNING</literal> clause must be provided, and the target
      relation must not have a conditional rule, nor an
      <literal>ALSO</literal> rule, nor an <literal>INSTEAD</literal> rule
      that expands to multiple statements.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">filename</replaceable></term>
    <listitem>
     <para>
      The path name of the input or output file.  An input file name can be
      an absolute or relative path, but an output file name must be an absolute
      path.  Windows users might need to use an <literal>E''</literal> string and
      double any backslashes used in the path name.
     </para>
    </listitem>
   </varlistentry>

Title: COPY Command Details: File Access, Progress Reporting, and Parameters
Summary
The COPY command interacts with files via the PostgreSQL server, requiring accessibility for the PostgreSQL user. It supports executing programs for data input/output and transmitting data via client-server connection using STDIN/STDOUT. Progress is reported in the pg_stat_progress_copy view. Parameters include table name, column list, query, and filename. Error handling can be customized with ON_ERROR clause.