5
COPY: Table Backup
Backup
output of a table contents or a query results into a file, stream or program
Restore
insertion of rows from a file or input stream into an existing table
Server variant Client variant
SQL COPY command psql \COPY command
the file must be accessible the file must be accessible
to the postgres user to the user who has launched psql
on the server on the client
If you want to save only the contents of one table, you can use the COPY
command.
The command writes a table (or the result of an arbitrary query) either to a
file or to an output stream, or sends it as input to another program. You can
specify options such as format (plain text, csv or binary), field separator,
NULL string representaion, etc.
The opposite variant of the COPY command reads fields from a file or input
stream and inserts them into a table. The table isn’t cleared, the new rows
are simply appended to the existing ones.
The COPY command is significantly faster than similar INSERT commands,
because the client does not need to access the server repeatedly, and the
server does not have to analyze the commands multiple times.
In psql, there is a client version of the COPY command with a similar syntax.
Unlike the server version, which is an SQL command, the client version is a
psql command.
The file name in the SQL command corresponds to a file on the database
server. The user running PostgreSQL (usually postgres) must have access
to this file. In the client version, the file is accessed on the client, and only
the content is transmitted to the server.