Wiki / psql Commands / Data transfer and large objects
\copy
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. T…
Reading PostgreSQL 18.6.
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.
- Client
- psql 18.6
- Case-sensitive spelling
- \copy
Usage
\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 in this manual definition
| Command | Documented suffix modifiers |
|---|---|
| \copy | None listed in this signature |
Manual definition
\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[, ...] ) ]-
Performs a frontend (client) copy. This is an operation that runs an SQL
COPYcommand, 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
programis specified,commandis executed by psql and the data passed from or tocommandis 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 theCOPYcommand 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 orcount\ooption, writefrom pstdinorto pstdout.The syntax of this command is similar to that of the SQL
COPYcommand. All options other than the data source/destination are as specified forCOPY. Because of this, special parsing rules apply to the\copymeta-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 ... tois to use the SQLCOPY ... TO STDOUTcommand and terminate it with\gorfilename\g |. Unlikeprogram\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
COPYcommand 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.
Related entries
Documentation and source
Source build
- Version
- 18.6
- Build
- https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
- Source fingerprint
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8
Compare versions
PostgreSQL 13 → 14: changed.
--- 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": [
Compares recorded interfaces and attributes. Source fingerprints and build metadata are excluded; an absent sample is not proof of the introduction or removal release.
Related entries
Export JSON · Back to psql Commands · Recorded in PostgreSQL 10 through 20; the first sample is not necessarily its introduction.