gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

629 行
44KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>COPY</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-commit-prepared.html" title="COMMIT PREPARED" /><link rel="next" href="sql-create-access-method.html" title="CREATE ACCESS METHOD" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">COPY</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-commit-prepared.html" title="COMMIT PREPARED">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-create-access-method.html" title="CREATE ACCESS METHOD">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-COPY"><div class="titlepage"></div><a id="id-1.9.3.55.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">COPY</span></h2><p>COPY — copy data between a file and a table</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. COPY <em class="replaceable"><code>table_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ]
  4. FROM { '<em class="replaceable"><code>filename</code></em>' | PROGRAM '<em class="replaceable"><code>command</code></em>' | STDIN }
  5. [ [ WITH ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]
  6. [ WHERE <em class="replaceable"><code>condition</code></em> ]
  7. COPY { <em class="replaceable"><code>table_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ] | ( <em class="replaceable"><code>query</code></em> ) }
  8. TO { '<em class="replaceable"><code>filename</code></em>' | PROGRAM '<em class="replaceable"><code>command</code></em>' | STDOUT }
  9. [ [ WITH ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]
  10. <span class="phrase">where <em class="replaceable"><code>option</code></em> can be one of:</span>
  11. FORMAT <em class="replaceable"><code>format_name</code></em>
  12. FREEZE [ <em class="replaceable"><code>boolean</code></em> ]
  13. DELIMITER '<em class="replaceable"><code>delimiter_character</code></em>'
  14. NULL '<em class="replaceable"><code>null_string</code></em>'
  15. HEADER [ <em class="replaceable"><code>boolean</code></em> ]
  16. QUOTE '<em class="replaceable"><code>quote_character</code></em>'
  17. ESCAPE '<em class="replaceable"><code>escape_character</code></em>'
  18. FORCE_QUOTE { ( <em class="replaceable"><code>column_name</code></em> [, ...] ) | * }
  19. FORCE_NOT_NULL ( <em class="replaceable"><code>column_name</code></em> [, ...] )
  20. FORCE_NULL ( <em class="replaceable"><code>column_name</code></em> [, ...] )
  21. ENCODING '<em class="replaceable"><code>encoding_name</code></em>'
  22. </pre></div><div class="refsect1" id="id-1.9.3.55.5"><h2>Description</h2><p>
  23. <code class="command">COPY</code> moves data between
  24. <span class="productname">PostgreSQL</span> tables and standard file-system
  25. files. <code class="command">COPY TO</code> copies the contents of a table
  26. <span class="emphasis"><em>to</em></span> a file, while <code class="command">COPY FROM</code> copies
  27. data <span class="emphasis"><em>from</em></span> a file to a table (appending the data to
  28. whatever is in the table already). <code class="command">COPY TO</code>
  29. can also copy the results of a <code class="command">SELECT</code> query.
  30. </p><p>
  31. If a column list is specified, <code class="command">COPY TO</code> copies only
  32. the data in the specified columns to the file. For <code class="command">COPY
  33. FROM</code>, each field in the file is inserted, in order, into the
  34. specified column. Table columns not specified in the <code class="command">COPY
  35. FROM</code> column list will receive their default values.
  36. </p><p>
  37. <code class="command">COPY</code> with a file name instructs the
  38. <span class="productname">PostgreSQL</span> server to directly read from
  39. or write to a file. The file must be accessible by the
  40. <span class="productname">PostgreSQL</span> user (the user ID the server
  41. runs as) and the name must be specified from the viewpoint of the
  42. server. When <code class="literal">PROGRAM</code> is specified, the server
  43. executes the given command and reads from the standard output of the
  44. program, or writes to the standard input of the program. The command
  45. must be specified from the viewpoint of the server, and be executable
  46. by the <span class="productname">PostgreSQL</span> user. When
  47. <code class="literal">STDIN</code> or <code class="literal">STDOUT</code> is
  48. specified, data is transmitted via the connection between the
  49. client and the server.
  50. </p></div><div class="refsect1" id="id-1.9.3.55.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  51. The name (optionally schema-qualified) of an existing table.
  52. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  53. An optional list of columns to be copied. If no column list is
  54. specified, all columns of the table except generated columns will be
  55. copied.
  56. </p></dd><dt><span class="term"><em class="replaceable"><code>query</code></em></span></dt><dd><p>
  57. A <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a>, <a class="xref" href="sql-values.html" title="VALUES"><span class="refentrytitle">VALUES</span></a>,
  58. <a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a>, <a class="xref" href="sql-update.html" title="UPDATE"><span class="refentrytitle">UPDATE</span></a> or
  59. <a class="xref" href="sql-delete.html" title="DELETE"><span class="refentrytitle">DELETE</span></a> command whose results are to be
  60. copied. Note that parentheses are required around the query.
  61. </p><p>
  62. For <code class="command">INSERT</code>, <code class="command">UPDATE</code> and
  63. <code class="command">DELETE</code> queries a RETURNING clause must be provided,
  64. and the target relation must not have a conditional rule, nor
  65. an <code class="literal">ALSO</code> rule, nor an <code class="literal">INSTEAD</code> rule
  66. that expands to multiple statements.
  67. </p></dd><dt><span class="term"><em class="replaceable"><code>filename</code></em></span></dt><dd><p>
  68. The path name of the input or output file. An input file name can be
  69. an absolute or relative path, but an output file name must be an absolute
  70. path. Windows users might need to use an <code class="literal">E''</code> string and
  71. double any backslashes used in the path name.
  72. </p></dd><dt><span class="term"><code class="literal">PROGRAM</code></span></dt><dd><p>
  73. A command to execute. In <code class="command">COPY FROM</code>, the input is
  74. read from standard output of the command, and in <code class="command">COPY TO</code>,
  75. the output is written to the standard input of the command.
  76. </p><p>
  77. Note that the command is invoked by the shell, so if you need to pass
  78. any arguments to shell command that come from an untrusted source, you
  79. must be careful to strip or escape any special characters that might
  80. have a special meaning for the shell. For security reasons, it is best
  81. to use a fixed command string, or at least avoid passing any user input
  82. in it.
  83. </p></dd><dt><span class="term"><code class="literal">STDIN</code></span></dt><dd><p>
  84. Specifies that input comes from the client application.
  85. </p></dd><dt><span class="term"><code class="literal">STDOUT</code></span></dt><dd><p>
  86. Specifies that output goes to the client application.
  87. </p></dd><dt><span class="term"><em class="replaceable"><code>boolean</code></em></span></dt><dd><p>
  88. Specifies whether the selected option should be turned on or off.
  89. You can write <code class="literal">TRUE</code>, <code class="literal">ON</code>, or
  90. <code class="literal">1</code> to enable the option, and <code class="literal">FALSE</code>,
  91. <code class="literal">OFF</code>, or <code class="literal">0</code> to disable it. The
  92. <em class="replaceable"><code>boolean</code></em> value can also
  93. be omitted, in which case <code class="literal">TRUE</code> is assumed.
  94. </p></dd><dt><span class="term"><code class="literal">FORMAT</code></span></dt><dd><p>
  95. Selects the data format to be read or written:
  96. <code class="literal">text</code>,
  97. <code class="literal">csv</code> (Comma Separated Values),
  98. or <code class="literal">binary</code>.
  99. The default is <code class="literal">text</code>.
  100. </p></dd><dt><span class="term"><code class="literal">FREEZE</code></span></dt><dd><p>
  101. Requests copying the data with rows already frozen, just as they
  102. would be after running the <code class="command">VACUUM FREEZE</code> command.
  103. This is intended as a performance option for initial data loading.
  104. Rows will be frozen only if the table being loaded has been created
  105. or truncated in the current subtransaction, there are no cursors
  106. open and there are no older snapshots held by this transaction. It is
  107. currently not possible to perform a <code class="command">COPY FREEZE</code> on
  108. a partitioned table.
  109. </p><p>
  110. Note that all other sessions will immediately be able to see the data
  111. once it has been successfully loaded. This violates the normal rules
  112. of MVCC visibility and users specifying should be aware of the
  113. potential problems this might cause.
  114. </p></dd><dt><span class="term"><code class="literal">DELIMITER</code></span></dt><dd><p>
  115. Specifies the character that separates columns within each row
  116. (line) of the file. The default is a tab character in text format,
  117. a comma in <code class="literal">CSV</code> format.
  118. This must be a single one-byte character.
  119. This option is not allowed when using <code class="literal">binary</code> format.
  120. </p></dd><dt><span class="term"><code class="literal">NULL</code></span></dt><dd><p>
  121. Specifies the string that represents a null value. The default is
  122. <code class="literal">\N</code> (backslash-N) in text format, and an unquoted empty
  123. string in <code class="literal">CSV</code> format. You might prefer an
  124. empty string even in text format for cases where you don't want to
  125. distinguish nulls from empty strings.
  126. This option is not allowed when using <code class="literal">binary</code> format.
  127. </p><div class="note"><h3 class="title">Note</h3><p>
  128. When using <code class="command">COPY FROM</code>, any data item that matches
  129. this string will be stored as a null value, so you should make
  130. sure that you use the same string as you used with
  131. <code class="command">COPY TO</code>.
  132. </p></div></dd><dt><span class="term"><code class="literal">HEADER</code></span></dt><dd><p>
  133. Specifies that the file contains a header line with the names of each
  134. column in the file. On output, the first line contains the column
  135. names from the table, and on input, the first line is ignored.
  136. This option is allowed only when using <code class="literal">CSV</code> format.
  137. </p></dd><dt><span class="term"><code class="literal">QUOTE</code></span></dt><dd><p>
  138. Specifies the quoting character to be used when a data value is quoted.
  139. The default is double-quote.
  140. This must be a single one-byte character.
  141. This option is allowed only when using <code class="literal">CSV</code> format.
  142. </p></dd><dt><span class="term"><code class="literal">ESCAPE</code></span></dt><dd><p>
  143. Specifies the character that should appear before a
  144. data character that matches the <code class="literal">QUOTE</code> value.
  145. The default is the same as the <code class="literal">QUOTE</code> value (so that
  146. the quoting character is doubled if it appears in the data).
  147. This must be a single one-byte character.
  148. This option is allowed only when using <code class="literal">CSV</code> format.
  149. </p></dd><dt><span class="term"><code class="literal">FORCE_QUOTE</code></span></dt><dd><p>
  150. Forces quoting to be
  151. used for all non-<code class="literal">NULL</code> values in each specified column.
  152. <code class="literal">NULL</code> output is never quoted. If <code class="literal">*</code> is specified,
  153. non-<code class="literal">NULL</code> values will be quoted in all columns.
  154. This option is allowed only in <code class="command">COPY TO</code>, and only when
  155. using <code class="literal">CSV</code> format.
  156. </p></dd><dt><span class="term"><code class="literal">FORCE_NOT_NULL</code></span></dt><dd><p>
  157. Do not match the specified columns' values against the null string.
  158. In the default case where the null string is empty, this means that
  159. empty values will be read as zero-length strings rather than nulls,
  160. even when they are not quoted.
  161. This option is allowed only in <code class="command">COPY FROM</code>, and only when
  162. using <code class="literal">CSV</code> format.
  163. </p></dd><dt><span class="term"><code class="literal">FORCE_NULL</code></span></dt><dd><p>
  164. Match the specified columns' values against the null string, even
  165. if it has been quoted, and if a match is found set the value to
  166. <code class="literal">NULL</code>. In the default case where the null string is empty,
  167. this converts a quoted empty string into NULL.
  168. This option is allowed only in <code class="command">COPY FROM</code>, and only when
  169. using <code class="literal">CSV</code> format.
  170. </p></dd><dt><span class="term"><code class="literal">ENCODING</code></span></dt><dd><p>
  171. Specifies that the file is encoded in the <em class="replaceable"><code>encoding_name</code></em>. If this option is
  172. omitted, the current client encoding is used. See the Notes below
  173. for more details.
  174. </p></dd><dt><span class="term"><code class="literal">WHERE</code></span></dt><dd><p>
  175. The optional <code class="literal">WHERE</code> clause has the general form
  176. </p><pre class="synopsis">
  177. WHERE <em class="replaceable"><code>condition</code></em>
  178. </pre><p>
  179. where <em class="replaceable"><code>condition</code></em> is
  180. any expression that evaluates to a result of type
  181. <code class="type">boolean</code>. Any row that does not satisfy this
  182. condition will not be inserted to the table. A row satisfies the
  183. condition if it returns true when the actual row values are
  184. substituted for any variable references.
  185. </p><p>
  186. Currently, subqueries are not allowed in <code class="literal">WHERE</code>
  187. expressions, and the evaluation does not see any changes made by the
  188. <code class="command">COPY</code> itself (this matters when the expression
  189. contains calls to <code class="literal">VOLATILE</code> functions).
  190. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.55.7"><h2>Outputs</h2><p>
  191. On successful completion, a <code class="command">COPY</code> command returns a command
  192. tag of the form
  193. </p><pre class="screen">
  194. COPY <em class="replaceable"><code>count</code></em>
  195. </pre><p>
  196. The <em class="replaceable"><code>count</code></em> is the number
  197. of rows copied.
  198. </p><div class="note"><h3 class="title">Note</h3><p>
  199. <span class="application">psql</span> will print this command tag only if the command
  200. was not <code class="literal">COPY ... TO STDOUT</code>, or the
  201. equivalent <span class="application">psql</span> meta-command
  202. <code class="literal">\copy ... to stdout</code>. This is to prevent confusing the
  203. command tag with the data that was just printed.
  204. </p></div></div><div class="refsect1" id="id-1.9.3.55.8"><h2>Notes</h2><p>
  205. <code class="command">COPY TO</code> can only be used with plain tables, not
  206. with views. However, you can write <code class="literal">COPY (SELECT * FROM
  207. <em class="replaceable"><code>viewname</code></em>) TO ...</code>
  208. to copy the current contents of a view.
  209. </p><p>
  210. <code class="command">COPY FROM</code> can be used with plain, foreign, or
  211. partitioned tables or with views that have
  212. <code class="literal">INSTEAD OF INSERT</code> triggers.
  213. </p><p>
  214. <code class="command">COPY</code> only deals with the specific table named;
  215. it does not copy data to or from child tables. Thus for example
  216. <code class="literal">COPY <em class="replaceable"><code>table</code></em> TO</code>
  217. shows the same data as <code class="literal">SELECT * FROM ONLY <em class="replaceable"><code>table</code></em></code>. But <code class="literal">COPY
  218. (SELECT * FROM <em class="replaceable"><code>table</code></em>) TO ...</code>
  219. can be used to dump all of the data in an inheritance hierarchy.
  220. </p><p>
  221. You must have select privilege on the table
  222. whose values are read by <code class="command">COPY TO</code>, and
  223. insert privilege on the table into which values
  224. are inserted by <code class="command">COPY FROM</code>. It is sufficient
  225. to have column privileges on the column(s) listed in the command.
  226. </p><p>
  227. If row-level security is enabled for the table, the relevant
  228. <code class="command">SELECT</code> policies will apply to <code class="literal">COPY
  229. <em class="replaceable"><code>table</code></em> TO</code> statements.
  230. Currently, <code class="command">COPY FROM</code> is not supported for tables
  231. with row-level security. Use equivalent <code class="command">INSERT</code>
  232. statements instead.
  233. </p><p>
  234. Files named in a <code class="command">COPY</code> command are read or written
  235. directly by the server, not by the client application. Therefore,
  236. they must reside on or be accessible to the database server machine,
  237. not the client. They must be accessible to and readable or writable
  238. by the <span class="productname">PostgreSQL</span> user (the user ID the
  239. server runs as), not the client. Similarly,
  240. the command specified with <code class="literal">PROGRAM</code> is executed directly
  241. by the server, not by the client application, must be executable by the
  242. <span class="productname">PostgreSQL</span> user.
  243. <code class="command">COPY</code> naming a file or command is only allowed to
  244. database superusers or users who are granted one of the default roles
  245. <code class="literal">pg_read_server_files</code>,
  246. <code class="literal">pg_write_server_files</code>,
  247. or <code class="literal">pg_execute_server_program</code>, since it allows reading
  248. or writing any file or running a program that the server has privileges to
  249. access.
  250. </p><p>
  251. Do not confuse <code class="command">COPY</code> with the
  252. <span class="application">psql</span> instruction
  253. <code class="command"><a class="link" href="app-psql.html#APP-PSQL-META-COMMANDS-COPY">\copy</a></code>. <code class="command">\copy</code> invokes
  254. <code class="command">COPY FROM STDIN</code> or <code class="command">COPY TO
  255. STDOUT</code>, and then fetches/stores the data in a file
  256. accessible to the <span class="application">psql</span> client. Thus,
  257. file accessibility and access rights depend on the client rather
  258. than the server when <code class="command">\copy</code> is used.
  259. </p><p>
  260. It is recommended that the file name used in <code class="command">COPY</code>
  261. always be specified as an absolute path. This is enforced by the
  262. server in the case of <code class="command">COPY TO</code>, but for
  263. <code class="command">COPY FROM</code> you do have the option of reading from
  264. a file specified by a relative path. The path will be interpreted
  265. relative to the working directory of the server process (normally
  266. the cluster's data directory), not the client's working directory.
  267. </p><p>
  268. Executing a command with <code class="literal">PROGRAM</code> might be restricted
  269. by the operating system's access control mechanisms, such as SELinux.
  270. </p><p>
  271. <code class="command">COPY FROM</code> will invoke any triggers and check
  272. constraints on the destination table. However, it will not invoke rules.
  273. </p><p>
  274. For identity columns, the <code class="command">COPY FROM</code> command will always
  275. write the column values provided in the input data, like
  276. the <code class="command">INSERT</code> option <code class="literal">OVERRIDING SYSTEM
  277. VALUE</code>.
  278. </p><p>
  279. <code class="command">COPY</code> input and output is affected by
  280. <code class="varname">DateStyle</code>. To ensure portability to other
  281. <span class="productname">PostgreSQL</span> installations that might use
  282. non-default <code class="varname">DateStyle</code> settings,
  283. <code class="varname">DateStyle</code> should be set to <code class="literal">ISO</code> before
  284. using <code class="command">COPY TO</code>. It is also a good idea to avoid dumping
  285. data with <code class="varname">IntervalStyle</code> set to
  286. <code class="literal">sql_standard</code>, because negative interval values might be
  287. misinterpreted by a server that has a different setting for
  288. <code class="varname">IntervalStyle</code>.
  289. </p><p>
  290. Input data is interpreted according to <code class="literal">ENCODING</code>
  291. option or the current client encoding, and output data is encoded
  292. in <code class="literal">ENCODING</code> or the current client encoding, even
  293. if the data does not pass through the client but is read from or
  294. written to a file directly by the server.
  295. </p><p>
  296. <code class="command">COPY</code> stops operation at the first error. This
  297. should not lead to problems in the event of a <code class="command">COPY
  298. TO</code>, but the target table will already have received
  299. earlier rows in a <code class="command">COPY FROM</code>. These rows will not
  300. be visible or accessible, but they still occupy disk space. This might
  301. amount to a considerable amount of wasted disk space if the failure
  302. happened well into a large copy operation. You might wish to invoke
  303. <code class="command">VACUUM</code> to recover the wasted space.
  304. </p><p>
  305. <code class="literal">FORCE_NULL</code> and <code class="literal">FORCE_NOT_NULL</code> can be used
  306. simultaneously on the same column. This results in converting quoted
  307. null strings to null values and unquoted null strings to empty strings.
  308. </p></div><div class="refsect1" id="id-1.9.3.55.9"><h2>File Formats</h2><div class="refsect2" id="id-1.9.3.55.9.2"><h3>Text Format</h3><p>
  309. When the <code class="literal">text</code> format is used,
  310. the data read or written is a text file with one line per table row.
  311. Columns in a row are separated by the delimiter character.
  312. The column values themselves are strings generated by the
  313. output function, or acceptable to the input function, of each
  314. attribute's data type. The specified null string is used in
  315. place of columns that are null.
  316. <code class="command">COPY FROM</code> will raise an error if any line of the
  317. input file contains more or fewer columns than are expected.
  318. </p><p>
  319. End of data can be represented by a single line containing just
  320. backslash-period (<code class="literal">\.</code>). An end-of-data marker is
  321. not necessary when reading from a file, since the end of file
  322. serves perfectly well; it is needed only when copying data to or from
  323. client applications using pre-3.0 client protocol.
  324. </p><p>
  325. Backslash characters (<code class="literal">\</code>) can be used in the
  326. <code class="command">COPY</code> data to quote data characters that might
  327. otherwise be taken as row or column delimiters. In particular, the
  328. following characters <span class="emphasis"><em>must</em></span> be preceded by a backslash if
  329. they appear as part of a column value: backslash itself,
  330. newline, carriage return, and the current delimiter character.
  331. </p><p>
  332. The specified null string is sent by <code class="command">COPY TO</code> without
  333. adding any backslashes; conversely, <code class="command">COPY FROM</code> matches
  334. the input against the null string before removing backslashes. Therefore,
  335. a null string such as <code class="literal">\N</code> cannot be confused with
  336. the actual data value <code class="literal">\N</code> (which would be represented
  337. as <code class="literal">\\N</code>).
  338. </p><p>
  339. The following special backslash sequences are recognized by
  340. <code class="command">COPY FROM</code>:
  341. </p><div class="informaltable"><table class="informaltable" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Sequence</th><th>Represents</th></tr></thead><tbody><tr><td><code class="literal">\b</code></td><td>Backspace (ASCII 8)</td></tr><tr><td><code class="literal">\f</code></td><td>Form feed (ASCII 12)</td></tr><tr><td><code class="literal">\n</code></td><td>Newline (ASCII 10)</td></tr><tr><td><code class="literal">\r</code></td><td>Carriage return (ASCII 13)</td></tr><tr><td><code class="literal">\t</code></td><td>Tab (ASCII 9)</td></tr><tr><td><code class="literal">\v</code></td><td>Vertical tab (ASCII 11)</td></tr><tr><td><code class="literal">\</code><em class="replaceable"><code>digits</code></em></td><td>Backslash followed by one to three octal digits specifies
  342. the character with that numeric code</td></tr><tr><td><code class="literal">\x</code><em class="replaceable"><code>digits</code></em></td><td>Backslash <code class="literal">x</code> followed by one or two hex digits specifies
  343. the character with that numeric code</td></tr></tbody></table></div><p>
  344. Presently, <code class="command">COPY TO</code> will never emit an octal or
  345. hex-digits backslash sequence, but it does use the other sequences
  346. listed above for those control characters.
  347. </p><p>
  348. Any other backslashed character that is not mentioned in the above table
  349. will be taken to represent itself. However, beware of adding backslashes
  350. unnecessarily, since that might accidentally produce a string matching the
  351. end-of-data marker (<code class="literal">\.</code>) or the null string (<code class="literal">\N</code> by
  352. default). These strings will be recognized before any other backslash
  353. processing is done.
  354. </p><p>
  355. It is strongly recommended that applications generating <code class="command">COPY</code> data convert
  356. data newlines and carriage returns to the <code class="literal">\n</code> and
  357. <code class="literal">\r</code> sequences respectively. At present it is
  358. possible to represent a data carriage return by a backslash and carriage
  359. return, and to represent a data newline by a backslash and newline.
  360. However, these representations might not be accepted in future releases.
  361. They are also highly vulnerable to corruption if the <code class="command">COPY</code> file is
  362. transferred across different machines (for example, from Unix to Windows
  363. or vice versa).
  364. </p><p>
  365. <code class="command">COPY TO</code> will terminate each row with a Unix-style
  366. newline (<span class="quote">“<span class="quote"><code class="literal">\n</code></span>”</span>). Servers running on Microsoft Windows instead
  367. output carriage return/newline (<span class="quote">“<span class="quote"><code class="literal">\r\n</code></span>”</span>), but only for
  368. <code class="command">COPY</code> to a server file; for consistency across platforms,
  369. <code class="command">COPY TO STDOUT</code> always sends <span class="quote">“<span class="quote"><code class="literal">\n</code></span>”</span>
  370. regardless of server platform.
  371. <code class="command">COPY FROM</code> can handle lines ending with newlines,
  372. carriage returns, or carriage return/newlines. To reduce the risk of
  373. error due to un-backslashed newlines or carriage returns that were
  374. meant as data, <code class="command">COPY FROM</code> will complain if the line
  375. endings in the input are not all alike.
  376. </p></div><div class="refsect2" id="id-1.9.3.55.9.3"><h3>CSV Format</h3><p>
  377. This format option is used for importing and exporting the Comma
  378. Separated Value (<code class="literal">CSV</code>) file format used by many other
  379. programs, such as spreadsheets. Instead of the escaping rules used by
  380. <span class="productname">PostgreSQL</span>'s standard text format, it
  381. produces and recognizes the common CSV escaping mechanism.
  382. </p><p>
  383. The values in each record are separated by the <code class="literal">DELIMITER</code>
  384. character. If the value contains the delimiter character, the
  385. <code class="literal">QUOTE</code> character, the <code class="literal">NULL</code> string, a carriage
  386. return, or line feed character, then the whole value is prefixed and
  387. suffixed by the <code class="literal">QUOTE</code> character, and any occurrence
  388. within the value of a <code class="literal">QUOTE</code> character or the
  389. <code class="literal">ESCAPE</code> character is preceded by the escape character.
  390. You can also use <code class="literal">FORCE_QUOTE</code> to force quotes when outputting
  391. non-<code class="literal">NULL</code> values in specific columns.
  392. </p><p>
  393. The <code class="literal">CSV</code> format has no standard way to distinguish a
  394. <code class="literal">NULL</code> value from an empty string.
  395. <span class="productname">PostgreSQL</span>'s <code class="command">COPY</code> handles this by quoting.
  396. A <code class="literal">NULL</code> is output as the <code class="literal">NULL</code> parameter string
  397. and is not quoted, while a non-<code class="literal">NULL</code> value matching the
  398. <code class="literal">NULL</code> parameter string is quoted. For example, with the
  399. default settings, a <code class="literal">NULL</code> is written as an unquoted empty
  400. string, while an empty string data value is written with double quotes
  401. (<code class="literal">""</code>). Reading values follows similar rules. You can
  402. use <code class="literal">FORCE_NOT_NULL</code> to prevent <code class="literal">NULL</code> input
  403. comparisons for specific columns. You can also use
  404. <code class="literal">FORCE_NULL</code> to convert quoted null string data values to
  405. <code class="literal">NULL</code>.
  406. </p><p>
  407. Because backslash is not a special character in the <code class="literal">CSV</code>
  408. format, <code class="literal">\.</code>, the end-of-data marker, could also appear
  409. as a data value. To avoid any misinterpretation, a <code class="literal">\.</code>
  410. data value appearing as a lone entry on a line is automatically
  411. quoted on output, and on input, if quoted, is not interpreted as the
  412. end-of-data marker. If you are loading a file created by another
  413. application that has a single unquoted column and might have a
  414. value of <code class="literal">\.</code>, you might need to quote that value in the
  415. input file.
  416. </p><div class="note"><h3 class="title">Note</h3><p>
  417. In <code class="literal">CSV</code> format, all characters are significant. A quoted value
  418. surrounded by white space, or any characters other than
  419. <code class="literal">DELIMITER</code>, will include those characters. This can cause
  420. errors if you import data from a system that pads <code class="literal">CSV</code>
  421. lines with white space out to some fixed width. If such a situation
  422. arises you might need to preprocess the <code class="literal">CSV</code> file to remove
  423. the trailing white space, before importing the data into
  424. <span class="productname">PostgreSQL</span>.
  425. </p></div><div class="note"><h3 class="title">Note</h3><p>
  426. CSV format will both recognize and produce CSV files with quoted
  427. values containing embedded carriage returns and line feeds. Thus
  428. the files are not strictly one line per table row like text-format
  429. files.
  430. </p></div><div class="note"><h3 class="title">Note</h3><p>
  431. Many programs produce strange and occasionally perverse CSV files,
  432. so the file format is more a convention than a standard. Thus you
  433. might encounter some files that cannot be imported using this
  434. mechanism, and <code class="command">COPY</code> might produce files that other
  435. programs cannot process.
  436. </p></div></div><div class="refsect2" id="id-1.9.3.55.9.4"><h3>Binary Format</h3><p>
  437. The <code class="literal">binary</code> format option causes all data to be
  438. stored/read as binary format rather than as text. It is
  439. somewhat faster than the text and <code class="literal">CSV</code> formats,
  440. but a binary-format file is less portable across machine architectures and
  441. <span class="productname">PostgreSQL</span> versions.
  442. Also, the binary format is very data type specific; for example
  443. it will not work to output binary data from a <code class="type">smallint</code> column
  444. and read it into an <code class="type">integer</code> column, even though that would work
  445. fine in text format.
  446. </p><p>
  447. The <code class="literal">binary</code> file format consists
  448. of a file header, zero or more tuples containing the row data, and
  449. a file trailer. Headers and data are in network byte order.
  450. </p><div class="note"><h3 class="title">Note</h3><p>
  451. <span class="productname">PostgreSQL</span> releases before 7.4 used a
  452. different binary file format.
  453. </p></div><div class="refsect3" id="id-1.9.3.55.9.4.5"><h4>File Header</h4><p>
  454. The file header consists of 15 bytes of fixed fields, followed
  455. by a variable-length header extension area. The fixed fields are:
  456. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">Signature</span></dt><dd><p>
  457. 11-byte sequence <code class="literal">PGCOPY\n\377\r\n\0</code> — note that the zero byte
  458. is a required part of the signature. (The signature is designed to allow
  459. easy identification of files that have been munged by a non-8-bit-clean
  460. transfer. This signature will be changed by end-of-line-translation
  461. filters, dropped zero bytes, dropped high bits, or parity changes.)
  462. </p></dd><dt><span class="term">Flags field</span></dt><dd><p>
  463. 32-bit integer bit mask to denote important aspects of the file format. Bits
  464. are numbered from 0 (<acronym class="acronym">LSB</acronym>) to 31 (<acronym class="acronym">MSB</acronym>). Note that
  465. this field is stored in network byte order (most significant byte first),
  466. as are all the integer fields used in the file format. Bits
  467. 16-31 are reserved to denote critical file format issues; a reader
  468. should abort if it finds an unexpected bit set in this range. Bits 0-15
  469. are reserved to signal backwards-compatible format issues; a reader
  470. should simply ignore any unexpected bits set in this range. Currently
  471. only one flag bit is defined, and the rest must be zero:
  472. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">Bit 16</span></dt><dd><p>
  473. If 1, OIDs are included in the data; if 0, not. Oid system columns
  474. are not supported in <span class="productname">PostgreSQL</span>
  475. anymore, but the format still contains the indicator.
  476. </p></dd></dl></div></dd><dt><span class="term">Header extension area length</span></dt><dd><p>
  477. 32-bit integer, length in bytes of remainder of header, not including self.
  478. Currently, this is zero, and the first tuple follows
  479. immediately. Future changes to the format might allow additional data
  480. to be present in the header. A reader should silently skip over any header
  481. extension data it does not know what to do with.
  482. </p></dd></dl></div><p>
  483. </p><p>
  484. The header extension area is envisioned to contain a sequence of
  485. self-identifying chunks. The flags field is not intended to tell readers
  486. what is in the extension area. Specific design of header extension contents
  487. is left for a later release.
  488. </p><p>
  489. This design allows for both backwards-compatible header additions (add
  490. header extension chunks, or set low-order flag bits) and
  491. non-backwards-compatible changes (set high-order flag bits to signal such
  492. changes, and add supporting data to the extension area if needed).
  493. </p></div><div class="refsect3" id="id-1.9.3.55.9.4.6"><h4>Tuples</h4><p>
  494. Each tuple begins with a 16-bit integer count of the number of fields in the
  495. tuple. (Presently, all tuples in a table will have the same count, but that
  496. might not always be true.) Then, repeated for each field in the tuple, there
  497. is a 32-bit length word followed by that many bytes of field data. (The
  498. length word does not include itself, and can be zero.) As a special case,
  499. -1 indicates a NULL field value. No value bytes follow in the NULL case.
  500. </p><p>
  501. There is no alignment padding or any other extra data between fields.
  502. </p><p>
  503. Presently, all data values in a binary-format file are
  504. assumed to be in binary format (format code one). It is anticipated that a
  505. future extension might add a header field that allows per-column format codes
  506. to be specified.
  507. </p><p>
  508. To determine the appropriate binary format for the actual tuple data you
  509. should consult the <span class="productname">PostgreSQL</span> source, in
  510. particular the <code class="function">*send</code> and <code class="function">*recv</code> functions for
  511. each column's data type (typically these functions are found in the
  512. <code class="filename">src/backend/utils/adt/</code> directory of the source
  513. distribution).
  514. </p><p>
  515. If OIDs are included in the file, the OID field immediately follows the
  516. field-count word. It is a normal field except that it's not included in the
  517. field-count. Note that oid system columns are not supported in current
  518. versions of <span class="productname">PostgreSQL</span>.
  519. </p></div><div class="refsect3" id="id-1.9.3.55.9.4.7"><h4>File Trailer</h4><p>
  520. The file trailer consists of a 16-bit integer word containing -1. This
  521. is easily distinguished from a tuple's field-count word.
  522. </p><p>
  523. A reader should report an error if a field-count word is neither -1
  524. nor the expected number of columns. This provides an extra
  525. check against somehow getting out of sync with the data.
  526. </p></div></div></div><div class="refsect1" id="id-1.9.3.55.10"><h2>Examples</h2><p>
  527. The following example copies a table to the client
  528. using the vertical bar (<code class="literal">|</code>) as the field delimiter:
  529. </p><pre class="programlisting">
  530. COPY country TO STDOUT (DELIMITER '|');
  531. </pre><p>
  532. </p><p>
  533. To copy data from a file into the <code class="literal">country</code> table:
  534. </p><pre class="programlisting">
  535. COPY country FROM '/usr1/proj/bray/sql/country_data';
  536. </pre><p>
  537. </p><p>
  538. To copy into a file just the countries whose names start with 'A':
  539. </p><pre class="programlisting">
  540. COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';
  541. </pre><p>
  542. </p><p>
  543. To copy into a compressed file, you can pipe the output through an external
  544. compression program:
  545. </p><pre class="programlisting">
  546. COPY country TO PROGRAM 'gzip &gt; /usr1/proj/bray/sql/country_data.gz';
  547. </pre><p>
  548. </p><p>
  549. Here is a sample of data suitable for copying into a table from
  550. <code class="literal">STDIN</code>:
  551. </p><pre class="programlisting">
  552. AF AFGHANISTAN
  553. AL ALBANIA
  554. DZ ALGERIA
  555. ZM ZAMBIA
  556. ZW ZIMBABWE
  557. </pre><p>
  558. Note that the white space on each line is actually a tab character.
  559. </p><p>
  560. The following is the same data, output in binary format.
  561. The data is shown after filtering through the
  562. Unix utility <code class="command">od -c</code>. The table has three columns;
  563. the first has type <code class="type">char(2)</code>, the second has type <code class="type">text</code>,
  564. and the third has type <code class="type">integer</code>. All the rows have a null value
  565. in the third column.
  566. </p><pre class="programlisting">
  567. 0000000 P G C O P Y \n 377 \r \n \0 \0 \0 \0 \0 \0
  568. 0000020 \0 \0 \0 \0 003 \0 \0 \0 002 A F \0 \0 \0 013 A
  569. 0000040 F G H A N I S T A N 377 377 377 377 \0 003
  570. 0000060 \0 \0 \0 002 A L \0 \0 \0 007 A L B A N I
  571. 0000100 A 377 377 377 377 \0 003 \0 \0 \0 002 D Z \0 \0 \0
  572. 0000120 007 A L G E R I A 377 377 377 377 \0 003 \0 \0
  573. 0000140 \0 002 Z M \0 \0 \0 006 Z A M B I A 377 377
  574. 0000160 377 377 \0 003 \0 \0 \0 002 Z W \0 \0 \0 \b Z I
  575. 0000200 M B A B W E 377 377 377 377 377 377
  576. </pre></div><div class="refsect1" id="id-1.9.3.55.11"><h2>Compatibility</h2><p>
  577. There is no <code class="command">COPY</code> statement in the SQL standard.
  578. </p><p>
  579. The following syntax was used before <span class="productname">PostgreSQL</span>
  580. version 9.0 and is still supported:
  581. </p><pre class="synopsis">
  582. COPY <em class="replaceable"><code>table_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ]
  583. FROM { '<em class="replaceable"><code>filename</code></em>' | STDIN }
  584. [ [ WITH ]
  585. [ BINARY ]
  586. [ DELIMITER [ AS ] '<em class="replaceable"><code>delimiter_character</code></em>' ]
  587. [ NULL [ AS ] '<em class="replaceable"><code>null_string</code></em>' ]
  588. [ CSV [ HEADER ]
  589. [ QUOTE [ AS ] '<em class="replaceable"><code>quote_character</code></em>' ]
  590. [ ESCAPE [ AS ] '<em class="replaceable"><code>escape_character</code></em>' ]
  591. [ FORCE NOT NULL <em class="replaceable"><code>column_name</code></em> [, ...] ] ] ]
  592. COPY { <em class="replaceable"><code>table_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ] | ( <em class="replaceable"><code>query</code></em> ) }
  593. TO { '<em class="replaceable"><code>filename</code></em>' | STDOUT }
  594. [ [ WITH ]
  595. [ BINARY ]
  596. [ DELIMITER [ AS ] '<em class="replaceable"><code>delimiter_character</code></em>' ]
  597. [ NULL [ AS ] '<em class="replaceable"><code>null_string</code></em>' ]
  598. [ CSV [ HEADER ]
  599. [ QUOTE [ AS ] '<em class="replaceable"><code>quote_character</code></em>' ]
  600. [ ESCAPE [ AS ] '<em class="replaceable"><code>escape_character</code></em>' ]
  601. [ FORCE QUOTE { <em class="replaceable"><code>column_name</code></em> [, ...] | * } ] ] ]
  602. </pre><p>
  603. Note that in this syntax, <code class="literal">BINARY</code> and <code class="literal">CSV</code> are
  604. treated as independent keywords, not as arguments of a <code class="literal">FORMAT</code>
  605. option.
  606. </p><p>
  607. The following syntax was used before <span class="productname">PostgreSQL</span>
  608. version 7.3 and is still supported:
  609. </p><pre class="synopsis">
  610. COPY [ BINARY ] <em class="replaceable"><code>table_name</code></em>
  611. FROM { '<em class="replaceable"><code>filename</code></em>' | STDIN }
  612. [ [USING] DELIMITERS '<em class="replaceable"><code>delimiter_character</code></em>' ]
  613. [ WITH NULL AS '<em class="replaceable"><code>null_string</code></em>' ]
  614. COPY [ BINARY ] <em class="replaceable"><code>table_name</code></em>
  615. TO { '<em class="replaceable"><code>filename</code></em>' | STDOUT }
  616. [ [USING] DELIMITERS '<em class="replaceable"><code>delimiter_character</code></em>' ]
  617. [ WITH NULL AS '<em class="replaceable"><code>null_string</code></em>' ]
  618. </pre></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-commit-prepared.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-create-access-method.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">COMMIT PREPARED </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE ACCESS METHOD</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1