COPY is an SQL command in PostgreSQL to move data between files and tables. There is also the meta-command \copy in the psql interface.
Besides the shell utilities pg_dump
and pg_restore
for backup and restore, there is also the SQL command COPY
in PostgreSQL to move data between files and database tables quickly. Several file formats are supported: text
, csv
and binary
.
The meta-command \copy
in the psql interface is a wrapper for the SQL command that reads and writes files local to the client (while COPY
is for files on the server).
Examples
Write all rows from a table employees
to a CSV file on the DB server with SQL COPY
:
COPY employees TO '/path/to/employees.csv' (FORMAT csv);
There are more examples for COPY
in the manual .
Read data from a CSV file on the client machine into a table with matching structure with \copy
in psql (appending to existing data):
\copy employees FROM '/path/to/employees.csv' (FORMAT csv);