Using Copy to export
suggest changeCOPY writes files on the database server. The role needs permission to read the table and to write server files. A table named user must be quoted, because USER is a reserved word.
Copy a table to standard output
COPY <tablename> TO STDOUT (DELIMITER '|');
To export the table "user" to standard output:
COPY "user" TO STDOUT (DELIMITER '|');
Copy a table to a file
COPY "user" TO '/home/user/user_data' WITH (DELIMITER '|');
Copy the output of an SQL statement to a file
COPY (<sql statement>) TO '<filename with path>';
COPY (SELECT * FROM "user" WHERE user_name LIKE 'A%') TO '/home/user/user_data';
Copy into a compressed file
COPY "user" TO PROGRAM 'gzip > /home/user/user_data.gz';
PostgreSQL runs the program on the server and writes the table data to that program’s standard input. The shell redirection stores gzip’s output in the .gz file.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents