UNLOAD or \copy lets you dump a Redshift table to a CSV file for easy sharing or downstream processing.
CSV files are portable, human-readable, and accepted by most BI tools. Exporting lets you archive data, debug queries locally, or feed machine-learning pipelines without granting live database access.
Inside Redshift you can use UNLOAD
to push data directly to Amazon S3.From outside, the \copy
meta-command in psql
streams rows to your laptop or a server.
Use UNLOAD
for large tables, scheduled jobs, or cross-region transfers because it writes in parallel to S3. Use \copy
for ad-hoc extracts under a few million rows when you already have a psql session open.
Grant the cluster an IAM role with s3:PutObject
.Choose a unique S3 URI and run the syntax below. Redshift writes multiple gzipped CSV parts by default.
UNLOAD ('SELECT id,name,email,created_at FROM public.customers')
TO 's3://my-bucket/exports/customers_'
CREDENTIALS 'aws_iam_role=arn:aws:iam::123456789012:role/redshift-s3-access'
DELIMITER ',' ADDQUOTES NULL AS 'NULL' ALLOWOVERWRITE PARALLEL OFF;
Open a terminal on a machine with network access to Redshift. Run psql
with -h
, -U
, and -d
.Then issue \copy
to stream the result set to a local CSV.
\copy (SELECT * FROM public.orders ORDER BY order_date) TO 'orders_2024-04-23.csv' CSV HEADER
Always quote text fields to protect embedded commas. Specify NULL AS
so downstream tools can distinguish real blanks. Turn PARALLEL OFF
when order matters in the file.
After UNLOAD, list the target S3 prefix and spot-check one part file.After \copy, count lines with wc -l
and compare to SELECT COUNT(*)
from the original table.
Filter columns in the SELECT list, encrypt the S3 bucket, and restrict IAM roles. For psql, write to an encrypted disk or tunnel through SSH.
.
Yes. Add PARALLEL OFF
so Redshift writes one part. For large tables this slows the export because it disables parallelism.
UNLOAD supports HEADER
; psql \copy
uses the HEADER
keyword. Both write column names as the first row.
UNLOAD can emit Parquet, JSON, or fixed-width; choose with the FORMAT AS
clause. \copy only writes text or binary.