PostgreSQL \copy Cheatsheet

Basic Syntax

\copy table_name [ (column1, column2, ...) ] FROM 'file_path' [ WITH ] (FORMAT format, DELIMITER 'delimiter', HEADER [boolean], ...)

Importing Data (FROM)

-- Basic CSV import
\copy table_name FROM '/path/to/file.csv' WITH (FORMAT csv, DELIMITER ',', HEADER true)
 
-- TSV file import
\copy table_name FROM '/path/to/file.tsv' WITH (FORMAT csv, DELIMITER '\t', HEADER true)
 
-- Import specific columns only
\copy table_name (col1, col2, col3) FROM '/path/to/file.csv' WITH CSV HEADER
 
-- Import with different encoding
\copy table_name FROM '/path/to/file.csv' WITH (FORMAT csv, ENCODING 'UTF8', HEADER true)

Exporting Data (TO)

-- Export to CSV
\copy table_name TO '/path/to/output.csv' WITH (FORMAT csv, DELIMITER ',', HEADER true)
 
-- Export query results
\copy (SELECT * FROM table WHERE condition) TO '/path/to/output.csv' WITH CSV HEADER
 
-- Export with custom delimiter
\copy table_name TO '/path/to/output.txt' WITH (FORMAT csv, DELIMITER '|', HEADER true)

Common Options

OptionDescriptionExample
FORMATFile format (csv, text, binary)FORMAT csv
DELIMITERField separator character`DELIMITER '
HEADERWhether file has header rowHEADER true
QUOTEQuoting characterQUOTE '"'
NULLHow NULL values are representedNULL '\N'
ENCODINGFile encodingENCODING 'LATIN1'
FORCE_QUOTEForce quoting for specified columnsFORCE_QUOTE (col1, col2)

Practical Examples

  1. Import from CSV with custom settings
\copy customers FROM '/data/import.csv' WITH (
    FORMAT csv,
    DELIMITER ';',
    HEADER true,
    NULL 'NULL',
    ENCODING 'WIN1252'
)
  1. Export query results to TSV
\copy (SELECT id, name FROM active_users WHERE signup_date > '2023-01-01') 
TO '/reports/active_users.tsv' 
WITH (FORMAT csv, DELIMITER '\t', HEADER true)
  1. Import with error handling
\copy products FROM '/tmp/products.csv' WITH CSV HEADER;
-- If errors occur:
\echo Import encountered errors
\set ON_ERROR_STOP on

Important Notes

  • \copy is a psql client command (not SQL), so it uses local file paths and your user’s permissions
  • For large files, consider using COPY (server-side) instead for better performance
  • Use absolute paths to avoid permission issues
  • The command must be on a single line (or use \ for line continuation)
  • For binary format, use FORMAT binary

Would you like me to add any specific use cases to this cheatsheet?