\copy
执行前端(客户端)复制。该操作会运行一个 SQL COPY 命令,但并不是由服务器读取或写入指定文件,而是由 psql 读取或写入文件,并在服务器与本地文件系统之间转送数据。这意味着文件可访问性和权限取决于本地用户,而不是服务器,也不需要 SQL 超级用户权限。
当前查看 PostgreSQL 18.6。
说明
执行前端(客户端)复制。该操作会运行一个 SQL COPY 命令,但并不是由服务器读取或写入指定文件,而是由 psql 读取或写入文件,并在服务器与本地文件系统之间转送数据。这意味着文件可访问性和权限取决于本地用户,而不是服务器,也不需要 SQL 超级用户权限。
- 客户端
- psql 18.6
- 区分大小写的写法
- \copy
用法
\copy { table [ ( column_list ) ] } from { 'filename' | program 'command' | stdin | pstdin } [ [ with ] ( option [, ...] ) ] [ where condition ] \copy { table [ ( column_list ) ] | ( query ) } to { 'filename' | program 'command' | stdout | pstdout } [ [ with ] ( option [, ...] ) ]本版手册中的写法
| 命令 | 手册中的后缀修饰符 |
|---|---|
| \copy | 此语法签名未列出 |
手册定义
\copy {table[ (column_list) ] }from{'filename'| program'command'| stdin | pstdin } [ [ with ] (option[, ...] ) ] [ wherecondition]\copy {table[ (column_list) ] | (query) }to{'filename'| program'command'| stdout | pstdout } [ [ with ] (option[, ...] ) ]-
执行前端(客户端)复制。该操作会运行一个SQL
COPY命令,但并不是由服务器读取或写入指定文件,而是由psql读取或写入文件,并在服务器与本地文件系统之间转送数据。这意味着文件可访问性和权限取决于本地用户,而不是服务器,也不需要 SQL 超级用户权限。当指定
program时,command由psql执行,传给command的数据或从其中读出的数据都会在服务器与客户端之间转送。再次强调,执行权限属于本地用户,而不是服务器,也不需要 SQL 超级用户权限。对于 \copy ... from stdin,从发出命令的同一输入源读取数据行,直到读取到仅含 \. 的一行或输入流到达 EOF。这适合在 SQL 脚本中内联填充表。对于 \copy ... to stdout,输出发往与 psql 命令输出相同的位置,且不打印 COPY count 命令状态,以免与数据行混淆。若要始终读写 psql 的标准输入或输出,而不受当前命令来源或 \o 选项影响,请使用 from pstdin 或 to pstdout。
该命令的语法与SQL
COPY命令类似。除数据源或目标之外,其他所有选项都与COPY命令中的指定相同。因此,特殊的解析规则适用于\copy元命令。与大多数其他元命令不同,整个剩余行始终被视为\copy的参数,参数中不执行变量插值或反引号扩展。提示
另一种获得与
\copy ... to相同结果的方法是使用SQLCOPY ... TO STDOUT命令,并以\g或filename\g |结束。与program\copy不同,这种方法允许命令跨越多行;此外,可以使用变量插值和反引号扩展。提示
这些操作不如以文件或程序作为数据源或目标的 SQL
COPY命令高效,因为所有数据都必须通过客户端/服务器连接传输。对于大量数据,使用 SQL 命令可能更合适。
相关条目
文档与源码
来源构建
- 版本
- 18.6
- 构建
- https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
- 来源指纹
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8
版本比较
PostgreSQL 13 → 14: 属性变化。
以下差异保留原始字段名与英文源描述。
--- PostgreSQL 13
+++ PostgreSQL 14
@@ -1,5 +1,5 @@
{
- "definition": "Performs a frontend (client) copy. This is an operation that runs an SQL COPY command, but instead of the server reading or writing the specified file, psql reads or writes the file and routes the data between the server and the local file system. This means that file accessibility and privileges are those of the local user, not the server, and no SQL superuser privileges are required. When program is specified, command is executed by psql and the data passed from or to command is routed between the server and the client. Again, the execution privileges are those of the local user, not the server, and no SQL superuser privileges are required. For \\copy ... from stdin , data rows are read from the same source that issued the command, continuing until \\. is read or the stream reaches EOF . This option is useful for populating tables in-line within a SQL script file. For \\copy ... to stdout , output is sent to the same place as psql command output, and the COPY count command status is not printed (since it might be confused with a data row). To read/write psql 's standard input or output regardless of the current command source or \\o option, write from pstdin or to pstdout . The syntax of this command is similar to that of the SQL COPY command. All options other than the data source/destination are as specified for COPY . Because of this, special parsing rules apply to the \\copy meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be the arguments of \\copy , and neither variable interpolation nor backquote expansion are performed in the arguments. Tip Another way to obtain the same result as \\copy ... to is to use the SQL COPY ... TO STDOUT command and terminate it with \\g filename or \\g | program . Unlike \\copy , this method allows the command to span multiple lines; also, variable interpolation and backquote expansion can be used. Tip These operations are not as efficient as the SQL COPY command with a file or program data source or destination, because all data must pass through the client/server connection. For large amounts of data the SQL command might be preferable. Also, because of this pass-through method, \\copy ... from in CSV mode will erroneously treat a \\. data value alone on a line as an end-of-input marker.",
+ "definition": "Performs a frontend (client) copy. This is an operation that runs an SQL COPY command, but instead of the server reading or writing the specified file, psql reads or writes the file and routes the data between the server and the local file system. This means that file accessibility and privileges are those of the local user, not the server, and no SQL superuser privileges are required. When program is specified, command is executed by psql and the data passed from or to command is routed between the server and the client. Again, the execution privileges are those of the local user, not the server, and no SQL superuser privileges are required. For \\copy ... from stdin , data rows are read from the same source that issued the command, continuing until \\. is read or the stream reaches EOF . This option is useful for populating tables in-line within an SQL script file. For \\copy ... to stdout , output is sent to the same place as psql command output, and the COPY count command status is not printed (since it might be confused with a data row). To read/write psql 's standard input or output regardless of the current command source or \\o option, write from pstdin or to pstdout . The syntax of this command is similar to that of the SQL COPY command. All options other than the data source/destination are as specified for COPY . Because of this, special parsing rules apply to the \\copy meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be the arguments of \\copy , and neither variable interpolation nor backquote expansion are performed in the arguments. Tip Another way to obtain the same result as \\copy ... to is to use the SQL COPY ... TO STDOUT command and terminate it with \\g filename or \\g | program . Unlike \\copy , this method allows the command to span multiple lines; also, variable interpolation and backquote expansion can be used. Tip These operations are not as efficient as the SQL COPY command with a file or program data source or destination, because all data must pass through the client/server connection. For large amounts of data the SQL command might be preferable. Also, because of this pass-through method, \\copy ... from in CSV mode will erroneously treat a \\. data value alone on a line as an end-of-input marker.",
"modifiers": [],
"signature": "\\copy { table [ ( column_list ) ] } from { 'filename' | program 'command' | stdin | pstdin } [ [ with ] ( option [, ...] ) ] [ where condition ] \\copy { table [ ( column_list ) ] | ( query ) } to { 'filename' | program 'command' | stdout | pstdout } [ [ with ] ( option [, ...] ) ]",
"spellings": [
比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。
相关条目
导出 JSON · 返回psql 命令 · 收录范围为 PostgreSQL 10 至 20;最早采样版本不一定是实际引入版本。