Home Explore Blog CI



postgresql

8th chunk of `doc/src/sgml/ref/copy.sgml`
0796b5219c14b7d24a7d4b5a5b3462f300964b1b72f511350000000100000fa6
 <literal>COPY (SELECT * FROM
    <replaceable class="parameter">table</replaceable>) TO</literal>.
   </para>

   <para>
    <command>COPY FROM</command> can be used with plain, foreign, or
    partitioned tables or with views that have
    <literal>INSTEAD OF INSERT</literal> triggers.
   </para>

   <para>
    You must have select privilege on the table
    whose values are read by <command>COPY TO</command>, and
    insert privilege on the table into which values
    are inserted by <command>COPY FROM</command>.  It is sufficient
    to have column privileges on the column(s) listed in the command.
   </para>

   <para>
    If row-level security is enabled for the table, the relevant
    <command>SELECT</command> policies will apply to <literal>COPY
    <replaceable class="parameter">table</replaceable> TO</literal> statements.
    Currently, <command>COPY FROM</command> is not supported for tables
    with row-level security. Use equivalent <command>INSERT</command>
    statements instead.
   </para>

   <para>
    Files named in a <command>COPY</command> command are read or written
    directly by the server, not by the client application. Therefore,
    they must reside on or be accessible to the database server machine,
    not the client. They must be accessible to and readable or writable
    by the <productname>PostgreSQL</productname> user (the user ID the
    server runs as), not the client. Similarly,
    the command specified with <literal>PROGRAM</literal> is executed directly
    by the server, not by the client application, must be executable by the
    <productname>PostgreSQL</productname> user.
    <command>COPY</command> naming a file or command is only allowed to
    database superusers or users who are granted one of the roles
    <literal>pg_read_server_files</literal>,
    <literal>pg_write_server_files</literal>,
    or <literal>pg_execute_server_program</literal>, since it allows reading
    or writing any file or running a program that the server has privileges to
    access.
   </para>

   <para>
    Do not confuse <command>COPY</command> with the
    <application>psql</application> instruction
    <command><link linkend="app-psql-meta-commands-copy">\copy</link></command>. <command>\copy</command> invokes
    <command>COPY FROM STDIN</command> or <command>COPY TO
    STDOUT</command>, and then fetches/stores the data in a file
    accessible to the <application>psql</application> client. Thus,
    file accessibility and access rights depend on the client rather
    than the server when <command>\copy</command> is used.
   </para>

   <para>
    It is recommended that the file name used in <command>COPY</command>
    always be specified as an absolute path. This is enforced by the
    server in the case of <command>COPY TO</command>, but for
    <command>COPY FROM</command> you do have the option of reading from
    a file specified by a relative path. The path will be interpreted
    relative to the working directory of the server process (normally
    the cluster's data directory), not the client's working directory.
   </para>

   <para>
    Executing a command with <literal>PROGRAM</literal> might be restricted
    by the operating system's access control mechanisms, such as SELinux.
   </para>

   <para>
    <command>COPY FROM</command> will invoke any triggers and check
    constraints on the destination table. However, it will not invoke rules.
   </para>

   <para>
    For identity columns, the <command>COPY FROM</command> command will always
    write the column values provided in the input data, like
    the <command>INSERT</command> option <literal>OVERRIDING SYSTEM
    VALUE</literal>.
   </para>

   <para>
    <command>COPY</command> input and output is affected by
    <varname>DateStyle</varname>. To ensure portability to other
    <productname>PostgreSQL</productname> installations that might use
    non-default <varname>DateStyle</varname> settings,
    <varname>DateStyle</varname> should

Title: COPY Command: Privileges, File Access, and Behavior
Summary
This section details important considerations for using the COPY command, including the necessary SELECT/INSERT privileges, the requirement for server-side file access, and the differences between COPY and psql's \copy meta-command. It emphasizes the server's role in file handling and program execution, the importance of absolute paths, and the command's interaction with triggers, constraints, rules, and identity columns. Finally, it notes the influence of DateStyle on COPY's input and output, recommending explicit setting for portability.