{"Entry":{"collection":"psql","key":"cmd-63-6f-70-79","name":"\\copy","aliases":["\\copy"],"metadata":{"aliases":["\\copy"],"category":"Data transfer and large objects","content_hash":"03eb58f6edb205fb74059436caad4fd5e2617754ed56a76aacf3658ff95584ad","imported_at":"2026-09-30T00:40:44.734295+08:00","name":"\\copy","name_zh":"","slug":"cmd-63-6f-70-79","summary":"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."}},"Definition":{"Collection":"psql","Key":"cmd-63-6f-70-79","SourceDatabase":"center","Version":"18","SourceTable":"psql_command","SourceKey":"cmd-63-6f-70-79","SourceRevision":"ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8","Facts":{"command":"\\copy","comparison_data":{"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 a line containing only \\. 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.","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":["\\copy"]},"comparison_hash":"e071767018f7d11f024bce4d06b14a4d3f8ba780a74d277363f0d263427ccc48","description":["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."],"facts":[{"label":"Client","value":"psql 18.6"},{"label":"Case-sensitive spelling","value":"\\copy"}],"manual_html":"\u003cdiv\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"APP-PSQL-META-COMMANDS-COPY\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\\copy { \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_list\u003c/code\u003e\u003c/em\u003e ) ] } \u003ccode class=\"literal\"\u003efrom\u003c/code\u003e { \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'filename'\u003c/code\u003e\u003c/em\u003e | program \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'command'\u003c/code\u003e\u003c/em\u003e | stdin | pstdin } [ [ with ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ] [ where \u003cem class=\"replaceable\"\u003e\u003ccode\u003econdition\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\\copy { \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_list\u003c/code\u003e\u003c/em\u003e ) ] | ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003equery\u003c/code\u003e\u003c/em\u003e ) } \u003ccode class=\"literal\"\u003eto\u003c/code\u003e { \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'filename'\u003c/code\u003e\u003c/em\u003e | program \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'command'\u003c/code\u003e\u003c/em\u003e | stdout | pstdout } [ [ with ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\n\u003cp\u003ePerforms a frontend (client) copy. This is an operation that runs an SQL \u003ca class=\"link\" href=\"/docs/18/sql-copy.html\" title=\"COPY\"\u003e\u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e\u003c/a\u003e command, but instead of the server reading or writing the specified file, \u003cspan class=\"application\"\u003epsql\u003c/span\u003e 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.\u003c/p\u003e\n\u003cp\u003eWhen \u003ccode class=\"literal\"\u003eprogram\u003c/code\u003e is specified, \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecommand\u003c/code\u003e\u003c/em\u003e is executed by \u003cspan class=\"application\"\u003epsql\u003c/span\u003e and the data passed from or to \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecommand\u003c/code\u003e\u003c/em\u003e 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.\u003c/p\u003e\n\u003cp\u003eFor \u003ccode class=\"literal\"\u003e\\copy ... from stdin\u003c/code\u003e, data rows are read from the same source that issued the command, continuing until a line containing only \u003ccode class=\"literal\"\u003e\\.\u003c/code\u003e is read or the stream reaches EOF. This option is useful for populating tables in-line within an SQL script file. For \u003ccode class=\"literal\"\u003e\\copy ... to stdout\u003c/code\u003e, output is sent to the same place as \u003cspan class=\"application\"\u003epsql\u003c/span\u003e command output, and the \u003ccode class=\"literal\"\u003eCOPY \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecount\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e command status is not printed (since it might be confused with a data row). To read/write \u003cspan class=\"application\"\u003epsql\u003c/span\u003e's standard input or output regardless of the current command source or \u003ccode class=\"literal\"\u003e\\o\u003c/code\u003e option, write \u003ccode class=\"literal\"\u003efrom pstdin\u003c/code\u003e or \u003ccode class=\"literal\"\u003eto pstdout\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eThe syntax of this command is similar to that of the SQL \u003ca class=\"link\" href=\"/docs/18/sql-copy.html\" title=\"COPY\"\u003e\u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e\u003c/a\u003e command. All options other than the data source/destination are as specified for \u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e. Because of this, special parsing rules apply to the \u003ccode class=\"command\"\u003e\\copy\u003c/code\u003e meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be the arguments of \u003ccode class=\"command\"\u003e\\copy\u003c/code\u003e, and neither variable interpolation nor backquote expansion are performed in the arguments.\u003c/p\u003e\n\u003cdiv class=\"tip\"\u003e\n\u003ch3 class=\"title\"\u003eTip\u003c/h3\u003e\n\u003cp\u003eAnother way to obtain the same result as \u003ccode class=\"literal\"\u003e\\copy ... to\u003c/code\u003e is to use the SQL \u003ccode class=\"literal\"\u003eCOPY ... TO STDOUT\u003c/code\u003e command and terminate it with \u003ccode class=\"literal\"\u003e\\g \u003cem class=\"replaceable\"\u003e\u003ccode\u003efilename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e or \u003ccode class=\"literal\"\u003e\\g |\u003cem class=\"replaceable\"\u003e\u003ccode\u003eprogram\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e. Unlike \u003ccode class=\"literal\"\u003e\\copy\u003c/code\u003e, this method allows the command to span multiple lines; also, variable interpolation and backquote expansion can be used.\u003c/p\u003e\n\u003c/div\u003e\n\u003cdiv class=\"tip\"\u003e\n\u003ch3 class=\"title\"\u003eTip\u003c/h3\u003e\n\u003cp\u003eThese operations are not as efficient as the SQL \u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e 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.\u003c/p\u003e\n\u003c/div\u003e\n\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","manual_path":"app-psql.html#APP-PSQL-META-COMMANDS-COPY","modifiers":[],"related":[{"label":"psql command-line options and complete manual","url":"/wiki/tool/psql/?v=18"}],"release":{"channel":"stable","evidence_kind":"English manual and source declarations","label":"18.6","major":"18","manifest":{"index":"index.html","major":"18","pages":1148,"pdf":{"A4":{"built_at":"2026-09-26","bytes":15865106,"pages":3154,"sha256":"19512c405da53f9f7fcf0abba359223aa65f021be025bf3411381918f92e3190","url":"/files/documentation/pdf/18/postgresql-18-A4.pdf"},"US":{"built_at":"2026-09-26","bytes":15748059,"pages":3328,"sha256":"facbe6c229e598b872d3d98bef53308f46e06746006fa4590de9a7de9dd46319","url":"/files/documentation/pdf/18/postgresql-18-US.pdf"}},"release":"18.6","source_mode":"en SGML built with pinned official archive","source_sha256":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f","source_url":"https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2","svg_assets":3,"tree":"18"},"ref":"https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2","revision":"ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8","source_sha256":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"},"sections":[],"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 [, ...] ) ]","sources":[{"anchor":"APP-PSQL-META-COMMANDS-COPY","file":"app-psql.html","label":"18.6 English manual · app-psql.html","sha256":"fa2a1d39294b8854318e82e93f71272c645ed88150ecc2786c28902c3f32782c","url":"/docs/18/app-psql.html#APP-PSQL-META-COMMANDS-COPY"}],"spellings":["\\copy"],"tables":[{"columns":[{"key":"command","label":"Command"},{"key":"modifiers","label":"Documented suffix modifiers"}],"key":"spellings","rows":[{"command":{"text":"\\copy","url":"/wiki/psql/cmd-63-6f-70-79/?v=18"},"modifiers":"None listed in this signature"}],"title":"Spellings in this manual definition"}]},"ManualEvidence":{"manual_path":"app-psql.html#APP-PSQL-META-COMMANDS-COPY","release":{"channel":"stable","evidence_kind":"English manual and source declarations","label":"18.6","major":"18","manifest":{"index":"index.html","major":"18","pages":1148,"pdf":{"A4":{"built_at":"2026-09-26","bytes":15865106,"pages":3154,"sha256":"19512c405da53f9f7fcf0abba359223aa65f021be025bf3411381918f92e3190","url":"/files/documentation/pdf/18/postgresql-18-A4.pdf"},"US":{"built_at":"2026-09-26","bytes":15748059,"pages":3328,"sha256":"facbe6c229e598b872d3d98bef53308f46e06746006fa4590de9a7de9dd46319","url":"/files/documentation/pdf/18/postgresql-18-US.pdf"}},"release":"18.6","source_mode":"en SGML built with pinned official archive","source_sha256":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f","source_url":"https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2","svg_assets":3,"tree":"18"},"ref":"https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2","revision":"ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8","source_sha256":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"},"sources":[{"anchor":"APP-PSQL-META-COMMANDS-COPY","file":"app-psql.html","label":"18.6 English manual · app-psql.html","sha256":"fa2a1d39294b8854318e82e93f71272c645ed88150ecc2786c28902c3f32782c","url":"/docs/18/app-psql.html#APP-PSQL-META-COMMANDS-COPY"}]},"MeasuredEvidence":{}},"Text":{"Collection":"psql","Key":"cmd-63-6f-70-79","SourceDatabase":"center","Version":"18","Locale":"en","Title":"\\copy","Summary":"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.","BodyHTML":"\u003cdiv\u003e\u003cdl\u003e\u003cdt id=\"APP-PSQL-META-COMMANDS-COPY\"\u003e\u003cspan\u003e\u003ccode\u003e\\copy { \u003cem\u003e\u003ccode\u003etable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem\u003e\u003ccode\u003ecolumn_list\u003c/code\u003e\u003c/em\u003e ) ] } \u003ccode\u003efrom\u003c/code\u003e { \u003cem\u003e\u003ccode\u003e\u0026#39;filename\u0026#39;\u003c/code\u003e\u003c/em\u003e | program \u003cem\u003e\u003ccode\u003e\u0026#39;command\u0026#39;\u003c/code\u003e\u003c/em\u003e | stdin | pstdin } [ [ with ] ( \u003cem\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ] [ where \u003cem\u003e\u003ccode\u003econdition\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003e\\copy { \u003cem\u003e\u003ccode\u003etable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem\u003e\u003ccode\u003ecolumn_list\u003c/code\u003e\u003c/em\u003e ) ] | ( \u003cem\u003e\u003ccode\u003equery\u003c/code\u003e\u003c/em\u003e ) } \u003ccode\u003eto\u003c/code\u003e { \u003cem\u003e\u003ccode\u003e\u0026#39;filename\u0026#39;\u003c/code\u003e\u003c/em\u003e | program \u003cem\u003e\u003ccode\u003e\u0026#39;command\u0026#39;\u003c/code\u003e\u003c/em\u003e | stdout | pstdout } [ [ with ] ( \u003cem\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\n\u003cp\u003ePerforms a frontend (client) copy. This is an operation that runs an SQL \u003ca href=\"/docs/18/sql-copy.html\" title=\"COPY\" rel=\"nofollow\"\u003e\u003ccode\u003eCOPY\u003c/code\u003e\u003c/a\u003e command, but instead of the server reading or writing the specified file, \u003cspan\u003epsql\u003c/span\u003e 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.\u003c/p\u003e\n\u003cp\u003eWhen \u003ccode\u003eprogram\u003c/code\u003e is specified, \u003cem\u003e\u003ccode\u003ecommand\u003c/code\u003e\u003c/em\u003e is executed by \u003cspan\u003epsql\u003c/span\u003e and the data passed from or to \u003cem\u003e\u003ccode\u003ecommand\u003c/code\u003e\u003c/em\u003e 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.\u003c/p\u003e\n\u003cp\u003eFor \u003ccode\u003e\\copy ... from stdin\u003c/code\u003e, data rows are read from the same source that issued the command, continuing until a line containing only \u003ccode\u003e\\.\u003c/code\u003e is read or the stream reaches EOF. This option is useful for populating tables in-line within an SQL script file. For \u003ccode\u003e\\copy ... to stdout\u003c/code\u003e, output is sent to the same place as \u003cspan\u003epsql\u003c/span\u003e command output, and the \u003ccode\u003eCOPY \u003cem\u003e\u003ccode\u003ecount\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e command status is not printed (since it might be confused with a data row). To read/write \u003cspan\u003epsql\u003c/span\u003e\u0026#39;s standard input or output regardless of the current command source or \u003ccode\u003e\\o\u003c/code\u003e option, write \u003ccode\u003efrom pstdin\u003c/code\u003e or \u003ccode\u003eto pstdout\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eThe syntax of this command is similar to that of the SQL \u003ca href=\"/docs/18/sql-copy.html\" title=\"COPY\" rel=\"nofollow\"\u003e\u003ccode\u003eCOPY\u003c/code\u003e\u003c/a\u003e command. All options other than the data source/destination are as specified for \u003ccode\u003eCOPY\u003c/code\u003e. Because of this, special parsing rules apply to the \u003ccode\u003e\\copy\u003c/code\u003e meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be the arguments of \u003ccode\u003e\\copy\u003c/code\u003e, and neither variable interpolation nor backquote expansion are performed in the arguments.\u003c/p\u003e\n\u003cdiv\u003e\n\u003ch3\u003eTip\u003c/h3\u003e\n\u003cp\u003eAnother way to obtain the same result as \u003ccode\u003e\\copy ... to\u003c/code\u003e is to use the SQL \u003ccode\u003eCOPY ... TO STDOUT\u003c/code\u003e command and terminate it with \u003ccode\u003e\\g \u003cem\u003e\u003ccode\u003efilename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e or \u003ccode\u003e\\g |\u003cem\u003e\u003ccode\u003eprogram\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e. Unlike \u003ccode\u003e\\copy\u003c/code\u003e, this method allows the command to span multiple lines; also, variable interpolation and backquote expansion can be used.\u003c/p\u003e\n\u003c/div\u003e\n\u003cdiv\u003e\n\u003ch3\u003eTip\u003c/h3\u003e\n\u003cp\u003eThese operations are not as efficient as the SQL \u003ccode\u003eCOPY\u003c/code\u003e 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.\u003c/p\u003e\n\u003c/div\u003e\n\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","SourceRevision":"ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8","ContentHash":"c18f6016fcdb2e9ad262d7c2461e5461243ab69a2e5c4d90bfb6993b50924b0f","Payload":{"description":["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."],"manual_html":"\u003cdiv\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"APP-PSQL-META-COMMANDS-COPY\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\\copy { \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_list\u003c/code\u003e\u003c/em\u003e ) ] } \u003ccode class=\"literal\"\u003efrom\u003c/code\u003e { \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'filename'\u003c/code\u003e\u003c/em\u003e | program \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'command'\u003c/code\u003e\u003c/em\u003e | stdin | pstdin } [ [ with ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ] [ where \u003cem class=\"replaceable\"\u003e\u003ccode\u003econdition\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\\copy { \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_list\u003c/code\u003e\u003c/em\u003e ) ] | ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003equery\u003c/code\u003e\u003c/em\u003e ) } \u003ccode class=\"literal\"\u003eto\u003c/code\u003e { \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'filename'\u003c/code\u003e\u003c/em\u003e | program \u003cem class=\"replaceable\"\u003e\u003ccode\u003e'command'\u003c/code\u003e\u003c/em\u003e | stdout | pstdout } [ [ with ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\n\u003cp\u003ePerforms a frontend (client) copy. This is an operation that runs an SQL \u003ca class=\"link\" href=\"/docs/18/sql-copy.html\" title=\"COPY\"\u003e\u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e\u003c/a\u003e command, but instead of the server reading or writing the specified file, \u003cspan class=\"application\"\u003epsql\u003c/span\u003e 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.\u003c/p\u003e\n\u003cp\u003eWhen \u003ccode class=\"literal\"\u003eprogram\u003c/code\u003e is specified, \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecommand\u003c/code\u003e\u003c/em\u003e is executed by \u003cspan class=\"application\"\u003epsql\u003c/span\u003e and the data passed from or to \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecommand\u003c/code\u003e\u003c/em\u003e 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.\u003c/p\u003e\n\u003cp\u003eFor \u003ccode class=\"literal\"\u003e\\copy ... from stdin\u003c/code\u003e, data rows are read from the same source that issued the command, continuing until a line containing only \u003ccode class=\"literal\"\u003e\\.\u003c/code\u003e is read or the stream reaches EOF. This option is useful for populating tables in-line within an SQL script file. For \u003ccode class=\"literal\"\u003e\\copy ... to stdout\u003c/code\u003e, output is sent to the same place as \u003cspan class=\"application\"\u003epsql\u003c/span\u003e command output, and the \u003ccode class=\"literal\"\u003eCOPY \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecount\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e command status is not printed (since it might be confused with a data row). To read/write \u003cspan class=\"application\"\u003epsql\u003c/span\u003e's standard input or output regardless of the current command source or \u003ccode class=\"literal\"\u003e\\o\u003c/code\u003e option, write \u003ccode class=\"literal\"\u003efrom pstdin\u003c/code\u003e or \u003ccode class=\"literal\"\u003eto pstdout\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eThe syntax of this command is similar to that of the SQL \u003ca class=\"link\" href=\"/docs/18/sql-copy.html\" title=\"COPY\"\u003e\u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e\u003c/a\u003e command. All options other than the data source/destination are as specified for \u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e. Because of this, special parsing rules apply to the \u003ccode class=\"command\"\u003e\\copy\u003c/code\u003e meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be the arguments of \u003ccode class=\"command\"\u003e\\copy\u003c/code\u003e, and neither variable interpolation nor backquote expansion are performed in the arguments.\u003c/p\u003e\n\u003cdiv class=\"tip\"\u003e\n\u003ch3 class=\"title\"\u003eTip\u003c/h3\u003e\n\u003cp\u003eAnother way to obtain the same result as \u003ccode class=\"literal\"\u003e\\copy ... to\u003c/code\u003e is to use the SQL \u003ccode class=\"literal\"\u003eCOPY ... TO STDOUT\u003c/code\u003e command and terminate it with \u003ccode class=\"literal\"\u003e\\g \u003cem class=\"replaceable\"\u003e\u003ccode\u003efilename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e or \u003ccode class=\"literal\"\u003e\\g |\u003cem class=\"replaceable\"\u003e\u003ccode\u003eprogram\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e. Unlike \u003ccode class=\"literal\"\u003e\\copy\u003c/code\u003e, this method allows the command to span multiple lines; also, variable interpolation and backquote expansion can be used.\u003c/p\u003e\n\u003c/div\u003e\n\u003cdiv class=\"tip\"\u003e\n\u003ch3 class=\"title\"\u003eTip\u003c/h3\u003e\n\u003cp\u003eThese operations are not as efficient as the SQL \u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e 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.\u003c/p\u003e\n\u003c/div\u003e\n\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","related":[{"label":"psql command-line options and complete manual","url":"/wiki/tool/psql/?v=18"}],"sections":[],"tables":[{"columns":[{"key":"command","label":"Command"},{"key":"modifiers","label":"Documented suffix modifiers"}],"key":"spellings","rows":[{"command":{"text":"\\copy","url":"/wiki/psql/cmd-63-6f-70-79/?v=18"},"modifiers":"None listed in this signature"}],"title":"Spellings in this manual definition"}]}},"RequestedLocale":"zh-Hans","Fallback":true,"Versions":["10","11","12","13","14","15","16","17","18","19","20"],"Locales":["en"],"Signatures":null,"Spellings":[{"Version":"18","Ordinal":0,"Command":"\\copy","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 [, ...] ) ]","Modifiers":[]}],"SQLState":null,"Evidence":null}
