COPY

Synopsis

Use the COPY statement to transfer data between tables and files. COPY TO copies from tables to files. COPY FROM copies from files to tables. COPY outputs the number of rows that were copied.

Syntax

  1. copy_from ::= COPY table_name [ ( column_name [ , ... ] ) ] FROM
  2. { 'filename' | PROGRAM 'command' | STDIN }
  3. [ [ WITH ] ( option [ , ... ] ) ]
  4. copy_to ::= COPY { table_name [ ( column_names ) ] | ( query ) } TO
  5. { 'filename' | PROGRAM 'command' | STDOUT }
  6. [ [ WITH ] ( option [ , ... ] ) ]
  7. copy_option ::= FORMAT format_name
  8. | OIDS [ boolean ]
  9. | FREEZE [ boolean ]
  10. | DELIMITER 'delimiter_character'
  11. | NULL 'null_string'
  12. | HEADER [ boolean ]
  13. | QUOTE 'quote_character'
  14. | ESCAPE 'escape_character'
  15. | FORCE_QUOTE { ( column_names ) | * }
  16. | FORCE_NOT_NULL ( column_names )
  17. | FORCE_NULL ( column_names )
  18. | ENCODING 'encoding_name'

copy_from

COPY - 图1

copy_to

COPY - 图2

copy_option

COPY - 图3

Semantics

table_name

Specify the table, optionally schema-qualified, to be copied.

column_name

Specify the list of columns to be copied. If not specified, then all columns of the table will be copied.

query

Specify a SELECT, VALUES, INSERT, UPDATE, or DELETE statement whose results are to be copied. For INSERT, UPDATE, and DELETE statements, a RETURNING clause must be provided.

filename

Specify the path of the file to be copied. An input file name can be an absolute or relative path, but an output file name must be an absolute path.

Examples

  • Errors are raised if the table does not exist.
  • COPY TO can only be used with regular tables.
  • COPY FROM can be used with tables, foreign tables, or views.