4 COPY — copy data between a file and a table
8 COPY table_name [ ( column_name [, ...] ) ]
9 FROM { 'filename' | PROGRAM 'command' | STDIN }
10 [ [ WITH ] ( option [, ...] ) ]
13 COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
14 TO { 'filename' | PROGRAM 'command' | STDOUT }
15 [ [ WITH ] ( option [, ...] ) ]
17 where option can be one of:
21 DELIMITER 'delimiter_character'
23 DEFAULT 'default_string'
24 HEADER [ boolean | MATCH ]
25 QUOTE 'quote_character'
26 ESCAPE 'escape_character'
27 FORCE_QUOTE { ( column_name [, ...] ) | * }
28 FORCE_NOT_NULL { ( column_name [, ...] ) | * }
29 FORCE_NULL { ( column_name [, ...] ) | * }
32 ENCODING 'encoding_name'
33 LOG_VERBOSITY verbosity
37 COPY moves data between PostgreSQL tables and standard file-system
38 files. COPY TO copies the contents of a table to a file, while COPY
39 FROM copies data from a file to a table (appending the data to whatever
40 is in the table already). COPY TO can also copy the results of a SELECT
43 If a column list is specified, COPY TO copies only the data in the
44 specified columns to the file. For COPY FROM, each field in the file is
45 inserted, in order, into the specified column. Table columns not
46 specified in the COPY FROM column list will receive their default
49 COPY with a file name instructs the PostgreSQL server to directly read
50 from or write to a file. The file must be accessible by the PostgreSQL
51 user (the user ID the server runs as) and the name must be specified
52 from the viewpoint of the server. When PROGRAM is specified, the server
53 executes the given command and reads from the standard output of the
54 program, or writes to the standard input of the program. The command
55 must be specified from the viewpoint of the server, and be executable
56 by the PostgreSQL user. When STDIN or STDOUT is specified, data is
57 transmitted via the connection between the client and the server.
59 Each backend running COPY will report its progress in the
60 pg_stat_progress_copy view. See Section 27.4.3 for details.
62 By default, COPY will fail if it encounters an error during processing.
63 For use cases where a best-effort attempt at loading the entire file is
64 desired, the ON_ERROR clause can be used to specify some other
70 The name (optionally schema-qualified) of an existing table.
73 An optional list of columns to be copied. If no column list is
74 specified, all columns of the table except generated columns
78 A SELECT, VALUES, INSERT, UPDATE, DELETE, or MERGE command whose
79 results are to be copied. Note that parentheses are required
82 For INSERT, UPDATE, DELETE, and MERGE queries a RETURNING clause
83 must be provided, and the target relation must not have a
84 conditional rule, nor an ALSO rule, nor an INSTEAD rule that
85 expands to multiple statements.
88 The path name of the input or output file. An input file name
89 can be an absolute or relative path, but an output file name
90 must be an absolute path. Windows users might need to use an E''
91 string and double any backslashes used in the path name.
94 A command to execute. In COPY FROM, the input is read from
95 standard output of the command, and in COPY TO, the output is
96 written to the standard input of the command.
98 Note that the command is invoked by the shell, so if you need to
99 pass any arguments that come from an untrusted source, you must
100 be careful to strip or escape any special characters that might
101 have a special meaning for the shell. For security reasons, it
102 is best to use a fixed command string, or at least avoid
103 including any user input in it.
106 Specifies that input comes from the client application.
109 Specifies that output goes to the client application.
112 Specifies whether the selected option should be turned on or
113 off. You can write TRUE, ON, or 1 to enable the option, and
114 FALSE, OFF, or 0 to disable it. The boolean value can also be
115 omitted, in which case TRUE is assumed.
118 Selects the data format to be read or written: text, csv (Comma
119 Separated Values), or binary. The default is text. See File
120 Formats below for details.
123 Requests copying the data with rows already frozen, just as they
124 would be after running the VACUUM FREEZE command. This is
125 intended as a performance option for initial data loading. Rows
126 will be frozen only if the table being loaded has been created
127 or truncated in the current subtransaction, there are no cursors
128 open and there are no older snapshots held by this transaction.
129 It is currently not possible to perform a COPY FREEZE on a
130 partitioned table or foreign table. This option is only allowed
133 Note that all other sessions will immediately be able to see the
134 data once it has been successfully loaded. This violates the
135 normal rules of MVCC visibility and users should be aware of the
136 potential problems this might cause.
139 Specifies the character that separates columns within each row
140 (line) of the file. The default is a tab character in text
141 format, a comma in CSV format. This must be a single one-byte
142 character. This option is not allowed when using binary format.
145 Specifies the string that represents a null value. The default
146 is \N (backslash-N) in text format, and an unquoted empty string
147 in CSV format. You might prefer an empty string even in text
148 format for cases where you don't want to distinguish nulls from
149 empty strings. This option is not allowed when using binary
154 When using COPY FROM, any data item that matches this string
155 will be stored as a null value, so you should make sure that you
156 use the same string as you used with COPY TO.
159 Specifies the string that represents a default value. Each time
160 the string is found in the input file, the default value of the
161 corresponding column will be used. This option is allowed only
162 in COPY FROM, and only when not using binary format.
165 Specifies that the file contains a header line with the names of
166 each column in the file. On output, the first line contains the
167 column names from the table. On input, the first line is
168 discarded when this option is set to true (or equivalent Boolean
169 value). If this option is set to MATCH, the number and names of
170 the columns in the header line must match the actual column
171 names of the table, in order; otherwise an error is raised. This
172 option is not allowed when using binary format. The MATCH option
173 is only valid for COPY FROM commands.
176 Specifies the quoting character to be used when a data value is
177 quoted. The default is double-quote. This must be a single
178 one-byte character. This option is allowed only when using CSV
182 Specifies the character that should appear before a data
183 character that matches the QUOTE value. The default is the same
184 as the QUOTE value (so that the quoting character is doubled if
185 it appears in the data). This must be a single one-byte
186 character. This option is allowed only when using CSV format.
189 Forces quoting to be used for all non-NULL values in each
190 specified column. NULL output is never quoted. If * is
191 specified, non-NULL values will be quoted in all columns. This
192 option is allowed only in COPY TO, and only when using CSV
196 Do not match the specified columns' values against the null
197 string. In the default case where the null string is empty, this
198 means that empty values will be read as zero-length strings
199 rather than nulls, even when they are not quoted. If * is
200 specified, the option will be applied to all columns. This
201 option is allowed only in COPY FROM, and only when using CSV
205 Match the specified columns' values against the null string,
206 even if it has been quoted, and if a match is found set the
207 value to NULL. In the default case where the null string is
208 empty, this converts a quoted empty string into NULL. If * is
209 specified, the option will be applied to all columns. This
210 option is allowed only in COPY FROM, and only when using CSV
214 Specifies how to behave when encountering an error converting a
215 column's input value into its data type. An error_action value
216 of stop means fail the command, while ignore means discard the
217 input row and continue with the next one. The default is stop.
219 The ignore option is applicable only for COPY FROM when the
220 FORMAT is text or csv.
222 A NOTICE message containing the ignored row count is emitted at
223 the end of the COPY FROM if at least one row was discarded. When
224 LOG_VERBOSITY option is set to verbose, a NOTICE message
225 containing the line of the input file and the column name whose
226 input conversion has failed is emitted for each discarded row.
227 When it is set to silent, no message is emitted regarding
231 Specifies the maximum number of errors tolerated while
232 converting a column's input value to its data type, when
233 ON_ERROR is set to ignore. If the input causes more errors than
234 the specified value, the COPY command fails, even with ON_ERROR
235 set to ignore. This clause must be used with ON_ERROR=ignore and
236 maxerror must be positive bigint. If not specified,
237 ON_ERROR=ignore allows an unlimited number of errors, meaning
238 COPY will skip all erroneous data.
241 Specifies that the file is encoded in the encoding_name. If this
242 option is omitted, the current client encoding is used. See the
243 Notes below for more details.
246 Specifies the amount of messages emitted by a COPY command:
247 default, verbose, or silent. If verbose is specified, additional
248 messages are emitted during processing. silent suppresses both
249 verbose and default messages.
251 This is currently used in COPY FROM command when ON_ERROR option
255 The optional WHERE clause has the general form
259 where condition is any expression that evaluates to a result of
260 type boolean. Any row that does not satisfy this condition will
261 not be inserted to the table. A row satisfies the condition if
262 it returns true when the actual row values are substituted for
263 any variable references.
265 Currently, subqueries are not allowed in WHERE expressions, and
266 the evaluation does not see any changes made by the COPY itself
267 (this matters when the expression contains calls to VOLATILE
272 On successful completion, a COPY command returns a command tag of the
276 The count is the number of rows copied.
280 psql will print this command tag only if the command was not COPY ...
281 TO STDOUT, or the equivalent psql meta-command \copy ... to stdout.
282 This is to prevent confusing the command tag with the data that was
287 COPY TO can be used with plain tables and populated materialized views.
288 For example, COPY table TO copies the same rows as SELECT * FROM ONLY
289 table. However it doesn't directly support other relation types, such
290 as partitioned tables, inheritance child tables, or views. To copy all
291 rows from such relations, use COPY (SELECT * FROM table) TO.
293 COPY FROM can be used with plain, foreign, or partitioned tables or
294 with views that have INSTEAD OF INSERT triggers.
296 You must have select privilege on the table whose values are read by
297 COPY TO, and insert privilege on the table into which values are
298 inserted by COPY FROM. It is sufficient to have column privileges on
299 the column(s) listed in the command.
301 If row-level security is enabled for the table, the relevant SELECT
302 policies will apply to COPY table TO statements. Currently, COPY FROM
303 is not supported for tables with row-level security. Use equivalent
304 INSERT statements instead.
306 Files named in a COPY command are read or written directly by the
307 server, not by the client application. Therefore, they must reside on
308 or be accessible to the database server machine, not the client. They
309 must be accessible to and readable or writable by the PostgreSQL user
310 (the user ID the server runs as), not the client. Similarly, the
311 command specified with PROGRAM is executed directly by the server, not
312 by the client application, must be executable by the PostgreSQL user.
313 COPY naming a file or command is only allowed to database superusers or
314 users who are granted one of the roles pg_read_server_files,
315 pg_write_server_files, or pg_execute_server_program, since it allows
316 reading or writing any file or running a program that the server has
317 privileges to access.
319 Do not confuse COPY with the psql instruction \copy. \copy invokes COPY
320 FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a
321 file accessible to the psql client. Thus, file accessibility and access
322 rights depend on the client rather than the server when \copy is used.
324 It is recommended that the file name used in COPY always be specified
325 as an absolute path. This is enforced by the server in the case of COPY
326 TO, but for COPY FROM you do have the option of reading from a file
327 specified by a relative path. The path will be interpreted relative to
328 the working directory of the server process (normally the cluster's
329 data directory), not the client's working directory.
331 Executing a command with PROGRAM might be restricted by the operating
332 system's access control mechanisms, such as SELinux.
334 COPY FROM will invoke any triggers and check constraints on the
335 destination table. However, it will not invoke rules.
337 For identity columns, the COPY FROM command will always write the
338 column values provided in the input data, like the INSERT option
339 OVERRIDING SYSTEM VALUE.
341 COPY input and output is affected by DateStyle. To ensure portability
342 to other PostgreSQL installations that might use non-default DateStyle
343 settings, DateStyle should be set to ISO before using COPY TO. It is
344 also a good idea to avoid dumping data with IntervalStyle set to
345 sql_standard, because negative interval values might be misinterpreted
346 by a server that has a different setting for IntervalStyle.
348 Input data is interpreted according to ENCODING option or the current
349 client encoding, and output data is encoded in ENCODING or the current
350 client encoding, even if the data does not pass through the client but
351 is read from or written to a file directly by the server.
353 The COPY FROM command physically inserts input rows into the table as
354 it progresses. If the command fails, these rows are left in a deleted
355 state; these rows will not be visible, but still occupy disk space.
356 This might amount to considerable wasted disk space if the failure
357 happened well into a large copy operation. VACUUM should be used to
358 recover the wasted space.
360 FORCE_NULL and FORCE_NOT_NULL can be used simultaneously on the same
361 column. This results in converting quoted null strings to null values
362 and unquoted null strings to empty strings.
368 When the text format is used, the data read or written is a text file
369 with one line per table row. Columns in a row are separated by the
370 delimiter character. The column values themselves are strings generated
371 by the output function, or acceptable to the input function, of each
372 attribute's data type. The specified null string is used in place of
373 columns that are null. COPY FROM will raise an error if any line of the
374 input file contains more or fewer columns than are expected.
376 End of data can be represented by a line containing just
377 backslash-period (\.). An end-of-data marker is not necessary when
378 reading from a file, since the end of file serves perfectly well; in
379 that context this provision exists only for backward compatibility.
380 However, psql uses \. to terminate a COPY FROM STDIN operation (that
381 is, reading in-line COPY data in an SQL script). In that context the
382 rule is needed to be able to end the operation before the end of the
385 Backslash characters (\) can be used in the COPY data to quote data
386 characters that might otherwise be taken as row or column delimiters.
387 In particular, the following characters must be preceded by a backslash
388 if they appear as part of a column value: backslash itself, newline,
389 carriage return, and the current delimiter character.
391 The specified null string is sent by COPY TO without adding any
392 backslashes; conversely, COPY FROM matches the input against the null
393 string before removing backslashes. Therefore, a null string such as \N
394 cannot be confused with the actual data value \N (which would be
397 The following special backslash sequences are recognized by COPY FROM:
399 \b Backspace (ASCII 8)
400 \f Form feed (ASCII 12)
401 \n Newline (ASCII 10)
402 \r Carriage return (ASCII 13)
404 \v Vertical tab (ASCII 11)
405 \digits Backslash followed by one to three octal digits specifies the
406 byte with that numeric code
407 \xdigits Backslash x followed by one or two hex digits specifies the
408 byte with that numeric code
410 Presently, COPY TO will never emit an octal or hex-digits backslash
411 sequence, but it does use the other sequences listed above for those
414 Any other backslashed character that is not mentioned in the above
415 table will be taken to represent itself. However, beware of adding
416 backslashes unnecessarily, since that might accidentally produce a
417 string matching the end-of-data marker (\.) or the null string (\N by
418 default). These strings will be recognized before any other backslash
421 It is strongly recommended that applications generating COPY data
422 convert data newlines and carriage returns to the \n and \r sequences
423 respectively. At present it is possible to represent a data carriage
424 return by a backslash and carriage return, and to represent a data
425 newline by a backslash and newline. However, these representations
426 might not be accepted in future releases. They are also highly
427 vulnerable to corruption if the COPY file is transferred across
428 different machines (for example, from Unix to Windows or vice versa).
430 All backslash sequences are interpreted after encoding conversion. The
431 bytes specified with the octal and hex-digit backslash sequences must
432 form valid characters in the database encoding.
434 COPY TO will terminate each row with a Unix-style newline (“\n”).
435 Servers running on Microsoft Windows instead output carriage
436 return/newline (“\r\n”), but only for COPY to a server file; for
437 consistency across platforms, COPY TO STDOUT always sends “\n”
438 regardless of server platform. COPY FROM can handle lines ending with
439 newlines, carriage returns, or carriage return/newlines. To reduce the
440 risk of error due to un-backslashed newlines or carriage returns that
441 were meant as data, COPY FROM will complain if the line endings in the
442 input are not all alike.
446 This format option is used for importing and exporting the Comma-
447 Separated Value (CSV) file format used by many other programs, such as
448 spreadsheets. Instead of the escaping rules used by PostgreSQL's
449 standard text format, it produces and recognizes the common CSV
452 The values in each record are separated by the DELIMITER character. If
453 the value contains the delimiter character, the QUOTE character, the
454 NULL string, a carriage return, or line feed character, then the whole
455 value is prefixed and suffixed by the QUOTE character, and any
456 occurrence within the value of a QUOTE character or the ESCAPE
457 character is preceded by the escape character. You can also use
458 FORCE_QUOTE to force quotes when outputting non-NULL values in specific
461 The CSV format has no standard way to distinguish a NULL value from an
462 empty string. PostgreSQL's COPY handles this by quoting. A NULL is
463 output as the NULL parameter string and is not quoted, while a non-NULL
464 value matching the NULL parameter string is quoted. For example, with
465 the default settings, a NULL is written as an unquoted empty string,
466 while an empty string data value is written with double quotes ("").
467 Reading values follows similar rules. You can use FORCE_NOT_NULL to
468 prevent NULL input comparisons for specific columns. You can also use
469 FORCE_NULL to convert quoted null string data values to NULL.
471 Because backslash is not a special character in the CSV format, the
472 end-of-data marker used in text mode (\.) is not normally treated as
473 special when reading CSV data. An exception is that psql will terminate
474 a COPY FROM STDIN operation (that is, reading in-line COPY data in an
475 SQL script) at a line containing only \., whether it is text or CSV
480 PostgreSQL versions before v18 always recognized unquoted \. as an
481 end-of-data marker, even when reading from a separate file. For
482 compatibility with older versions, COPY TO will quote \. when it's
483 alone on a line, even though this is no longer necessary.
487 In CSV format, all characters are significant. A quoted value
488 surrounded by white space, or any characters other than DELIMITER, will
489 include those characters. This can cause errors if you import data from
490 a system that pads CSV lines with white space out to some fixed width.
491 If such a situation arises you might need to preprocess the CSV file to
492 remove the trailing white space, before importing the data into
497 CSV format will both recognize and produce CSV files with quoted values
498 containing embedded carriage returns and line feeds. Thus the files are
499 not strictly one line per table row like text-format files.
503 Many programs produce strange and occasionally perverse CSV files, so
504 the file format is more a convention than a standard. Thus you might
505 encounter some files that cannot be imported using this mechanism, and
506 COPY might produce files that other programs cannot process.
510 The binary format option causes all data to be stored/read as binary
511 format rather than as text. It is somewhat faster than the text and CSV
512 formats, but a binary-format file is less portable across machine
513 architectures and PostgreSQL versions. Also, the binary format is very
514 data type specific; for example it will not work to output binary data
515 from a smallint column and read it into an integer column, even though
516 that would work fine in text format.
518 The binary file format consists of a file header, zero or more tuples
519 containing the row data, and a file trailer. Headers and data are in
524 PostgreSQL releases before 7.4 used a different binary file format.
528 The file header consists of 15 bytes of fixed fields, followed by a
529 variable-length header extension area. The fixed fields are:
532 11-byte sequence PGCOPY\n\377\r\n\0 — note that the zero byte is
533 a required part of the signature. (The signature is designed to
534 allow easy identification of files that have been munged by a
535 non-8-bit-clean transfer. This signature will be changed by
536 end-of-line-translation filters, dropped zero bytes, dropped
537 high bits, or parity changes.)
540 32-bit integer bit mask to denote important aspects of the file
541 format. Bits are numbered from 0 (LSB) to 31 (MSB). Note that
542 this field is stored in network byte order (most significant
543 byte first), as are all the integer fields used in the file
544 format. Bits 16–31 are reserved to denote critical file format
545 issues; a reader should abort if it finds an unexpected bit set
546 in this range. Bits 0–15 are reserved to signal
547 backwards-compatible format issues; a reader should simply
548 ignore any unexpected bits set in this range. Currently only one
549 flag bit is defined, and the rest must be zero:
552 If 1, OIDs are included in the data; if 0, not. Oid system
553 columns are not supported in PostgreSQL anymore, but the
554 format still contains the indicator.
556 Header extension area length
557 32-bit integer, length in bytes of remainder of header, not
558 including self. Currently, this is zero, and the first tuple
559 follows immediately. Future changes to the format might allow
560 additional data to be present in the header. A reader should
561 silently skip over any header extension data it does not know
564 The header extension area is envisioned to contain a sequence of
565 self-identifying chunks. The flags field is not intended to tell
566 readers what is in the extension area. Specific design of header
567 extension contents is left for a later release.
569 This design allows for both backwards-compatible header additions (add
570 header extension chunks, or set low-order flag bits) and
571 non-backwards-compatible changes (set high-order flag bits to signal
572 such changes, and add supporting data to the extension area if needed).
576 Each tuple begins with a 16-bit integer count of the number of fields
577 in the tuple. (Presently, all tuples in a table will have the same
578 count, but that might not always be true.) Then, repeated for each
579 field in the tuple, there is a 32-bit length word followed by that many
580 bytes of field data. (The length word does not include itself, and can
581 be zero.) As a special case, -1 indicates a NULL field value. No value
582 bytes follow in the NULL case.
584 There is no alignment padding or any other extra data between fields.
586 Presently, all data values in a binary-format file are assumed to be in
587 binary format (format code one). It is anticipated that a future
588 extension might add a header field that allows per-column format codes
591 To determine the appropriate binary format for the actual tuple data
592 you should consult the PostgreSQL source, in particular the *send and
593 *recv functions for each column's data type (typically these functions
594 are found in the src/backend/utils/adt/ directory of the source
597 If OIDs are included in the file, the OID field immediately follows the
598 field-count word. It is a normal field except that it's not included in
599 the field-count. Note that oid system columns are not supported in
600 current versions of PostgreSQL.
604 The file trailer consists of a 16-bit integer word containing -1. This
605 is easily distinguished from a tuple's field-count word.
607 A reader should report an error if a field-count word is neither -1 nor
608 the expected number of columns. This provides an extra check against
609 somehow getting out of sync with the data.
613 The following example copies a table to the client using the vertical
614 bar (|) as the field delimiter:
615 COPY country TO STDOUT (DELIMITER '|');
617 To copy data from a file into the country table:
618 COPY country FROM '/usr1/proj/bray/sql/country_data';
620 To copy into a file just the countries whose names start with 'A':
621 COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sq
622 l/a_list_countries.copy';
624 To copy into a compressed file, you can pipe the output through an
625 external compression program:
626 COPY country TO PROGRAM 'gzip > /usr1/proj/bray/sql/country_data.gz';
628 Here is a sample of data suitable for copying into a table from STDIN:
635 Note that the white space on each line is actually a tab character.
637 The following is the same data, output in binary format. The data is
638 shown after filtering through the Unix utility od -c. The table has
639 three columns; the first has type char(2), the second has type text,
640 and the third has type integer. All the rows have a null value in the
642 0000000 P G C O P Y \n 377 \r \n \0 \0 \0 \0 \0 \0
643 0000020 \0 \0 \0 \0 003 \0 \0 \0 002 A F \0 \0 \0 013 A
644 0000040 F G H A N I S T A N 377 377 377 377 \0 003
645 0000060 \0 \0 \0 002 A L \0 \0 \0 007 A L B A N I
646 0000100 A 377 377 377 377 \0 003 \0 \0 \0 002 D Z \0 \0 \0
647 0000120 007 A L G E R I A 377 377 377 377 \0 003 \0 \0
648 0000140 \0 002 Z M \0 \0 \0 006 Z A M B I A 377 377
649 0000160 377 377 \0 003 \0 \0 \0 002 Z W \0 \0 \0 \b Z I
650 0000200 M B A B W E 377 377 377 377 377 377
654 There is no COPY statement in the SQL standard.
656 The following syntax was used before PostgreSQL version 9.0 and is
658 COPY table_name [ ( column_name [, ...] ) ]
659 FROM { 'filename' | STDIN }
662 [ DELIMITER [ AS ] 'delimiter_character' ]
663 [ NULL [ AS ] 'null_string' ]
665 [ QUOTE [ AS ] 'quote_character' ]
666 [ ESCAPE [ AS ] 'escape_character' ]
667 [ FORCE NOT NULL column_name [, ...] ] ] ]
669 COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
670 TO { 'filename' | STDOUT }
673 [ DELIMITER [ AS ] 'delimiter_character' ]
674 [ NULL [ AS ] 'null_string' ]
676 [ QUOTE [ AS ] 'quote_character' ]
677 [ ESCAPE [ AS ] 'escape_character' ]
678 [ FORCE QUOTE { column_name [, ...] | * } ] ] ]
680 Note that in this syntax, BINARY and CSV are treated as independent
681 keywords, not as arguments of a FORMAT option.
683 The following syntax was used before PostgreSQL version 7.3 and is
685 COPY [ BINARY ] table_name
686 FROM { 'filename' | STDIN }
687 [ [USING] DELIMITERS 'delimiter_character' ]
688 [ WITH NULL AS 'null_string' ]
690 COPY [ BINARY ] table_name
691 TO { 'filename' | STDOUT }
692 [ [USING] DELIMITERS 'delimiter_character' ]
693 [ WITH NULL AS 'null_string' ]