@@ -23,11 +23,11 @@ PostgreSQL documentation
23
23
<refsynopsisdiv>
24
24
<synopsis>
25
25
COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
26
- FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
26
+ FROM { '<replaceable class="parameter">filename</replaceable>' | PROGRAM '<replaceable class="parameter">command</replaceable>' | STDIN }
27
27
[ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
28
28
29
29
COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
30
- TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
30
+ TO { '<replaceable class="parameter">filename</replaceable>' | PROGRAM '<replaceable class="parameter">command</replaceable>' | STDOUT }
31
31
[ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
32
32
33
33
<phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
@@ -72,6 +72,10 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
72
72
<productname>PostgreSQL</productname> server to directly read from
73
73
or write to a file. The file must be accessible to the server and
74
74
the name must be specified from the viewpoint of the server. When
75
+ <literal>PROGRAM</literal> is specified, the server executes the
76
+ given command, and reads from its standard input, or writes to its
77
+ standard output. The command must be specified from the viewpoint of the
78
+ server, and be executable by the <literal>postgres</> user. When
75
79
<literal>STDIN</literal> or <literal>STDOUT</literal> is
76
80
specified, data is transmitted via the connection between the
77
81
client and the server.
@@ -125,6 +129,25 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
125
129
</listitem>
126
130
</varlistentry>
127
131
132
+ <varlistentry>
133
+ <term><literal>PROGRAM</literal></term>
134
+ <listitem>
135
+ <para>
136
+ A command to execute. In <command>COPY FROM</command>, the input is
137
+ read from standard output of the command, and in <command>COPY TO</>,
138
+ the output is written to the standard input of the command.
139
+ </para>
140
+ <para>
141
+ Note that the command is invoked by the shell, so if you need to pass
142
+ any arguments to shell command that come from an untrusted source, you
143
+ must be careful to strip or escape any special characters that might
144
+ have a special meaning for the shell. For security reasons, it is best
145
+ to use a fixed command string, or at least avoid passing any user input
146
+ in it.
147
+ </para>
148
+ </listitem>
149
+ </varlistentry>
150
+
128
151
<varlistentry>
129
152
<term><literal>STDIN</literal></term>
130
153
<listitem>
@@ -367,9 +390,13 @@ COPY <replaceable class="parameter">count</replaceable>
367
390
they must reside on or be accessible to the database server machine,
368
391
not the client. They must be accessible to and readable or writable
369
392
by the <productname>PostgreSQL</productname> user (the user ID the
370
- server runs as), not the client. <command>COPY</command> naming a
371
- file is only allowed to database superusers, since it allows reading
372
- or writing any file that the server has privileges to access.
393
+ server runs as), not the client. Similarly,
394
+ the command specified with <literal>PROGRAM</literal> is executed directly
395
+ by the server, not by the client application, must be executable by the
396
+ <productname>PostgreSQL</productname> user.
397
+ <command>COPY</command> naming a file or command is only allowed to
398
+ database superusers, since it allows reading or writing any file that the
399
+ server has privileges to access.
373
400
</para>
374
401
375
402
<para>
@@ -393,6 +420,11 @@ COPY <replaceable class="parameter">count</replaceable>
393
420
the cluster's data directory), not the client's working directory.
394
421
</para>
395
422
423
+ <para>
424
+ Executing a command with <literal>PROGRAM</literal> might be restricted
425
+ by operating system's access control mechanisms, such as the SELinux.
426
+ </para>
427
+
396
428
<para>
397
429
<command>COPY FROM</command> will invoke any triggers and check
398
430
constraints on the destination table. However, it will not invoke rules.
@@ -841,6 +873,14 @@ COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sq
841
873
</programlisting>
842
874
</para>
843
875
876
+ <para>
877
+ To copy into a compressed file, you can pipe the output through an external
878
+ compression program:
879
+ <programlisting>
880
+ COPY country TO PROGRAM 'gzip > /usr1/proj/bray/sql/country_data.gz';
881
+ </programlisting>
882
+ </para>
883
+
844
884
<para>
845
885
Here is a sample of data suitable for copying into a table from
846
886
<literal>STDIN</literal>:
0 commit comments