# COPY TO

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: COPY TO
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: sql/sql-statements/copy-to
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-statements/copy-to.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-statements/copy-to.adoc
description: The COPY TO statement exports tables, specific columns, or query results to CSV files or to a client.
page-topic-type: reference
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-statements/copy-to.md -->

The `COPY TO` statement exports tables, specific columns, or query results to a CSV file or to the standard output.

> ⚠️ **WARNING**
>
> By default, the `COPY TO` command overwrites the destination file if it already exists. Ensure that the directory where you save the file has the necessary write permissions.

## [](#syntax)Syntax

```sql
COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
TO { 'file_path' | STDOUT }
[ WITH ( option [, ...] ) ];
```

-   `table_name`: Table containing the data to export.

-   `column_name`: Optional. Specific columns to export. If omitted, all columns are exported.

-   `query`: A `SELECT` statement to export specific results.

-   `file_path`: Path to the output file. Use `STDOUT` to send the data to the standard output stream.

-   `option`: One or more options. See [Options](#options).


## [](#options)Options

-   `FORMAT`: Output format. Only `CSV` is supported.

-   `DELIMITER`: Single-character string used to separate fields. Default is `,`.

-   `HEADER`: Whether to include a header row with column names. Accepts `ON`, `TRUE`, `1`, `OFF`, `FALSE`, or `0`. Default is `OFF`.

-   `NULL`: String used to represent `NULL` values. Default is the empty string.

-   Cloud storage credentials: Use `AWS_CRED`, `AZURE_CRED`, or `GCS_CRED` to authenticate when exporting to cloud storage. See [Export to cloud storage](#cloud-storage).


## [](#examples)Examples

The following examples use a table mapped to a Redpanda topic through a catalog. For information on setting up catalogs and tables, see [CREATE TABLE](https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-statements/create-table/).

### [](#export-all-columns-from-a-table)Export all columns from a table

Copy all columns in a table to a CSV file:

```sql
COPY default_redpanda_catalog=>employee_salary TO '/path/to/exportsalary.csv';
```

### [](#export-specific-columns-from-a-table)Export specific columns from a table

Specify the column names to export only those columns:

```sql
COPY default_redpanda_catalog=>employee_salary (empid, empname, empsalary) TO '/path/to/exportsalary.csv';
```

### [](#export-results-of-a-select-statement)Export results of a SELECT statement

Export only the rows that match a `WHERE` condition:

```sql
COPY (SELECT * FROM default_redpanda_catalog=>employee_salary WHERE empdept = 'Marketing') TO '/path/to/exportsalary.csv';
```

### [](#export-with-a-custom-delimiter)Export with a custom delimiter

Specify the delimiter using the `DELIMITER` option. Common delimiters include comma (`,`), semicolon (`;`), pipe (`|`), and dash (`-`).

```sql
COPY default_redpanda_catalog=>customer TO '/path/to/customerexport.csv' WITH (DELIMITER ';');
```

The exported CSV uses the specified delimiter:

```text
cust_id;cust_name
11001;Maya
11003;Ricky
11009;Sean
```

### [](#export-with-column-headers)Export with column headers

Include column names as a header row using `HEADER ON` (or `HEADER TRUE`, or `HEADER 1`):

```sql
COPY default_redpanda_catalog=>personal_details TO '/path/to/personalinfo.csv' WITH (HEADER ON);
```

The exported file includes a header row:

```text
id,first_name,last_name,gender
1,'Mark','Wheeler','M'
2,'Tom','Hanks','M'
3,'Jane','Hopper','F'
```

To omit the header (the default), use `HEADER OFF` (or `HEADER FALSE`, or `HEADER 0`).

### [](#export-with-a-null-replacement-string)Export with a NULL replacement string

Specify a string to replace `NULL` values in the exported file:

```sql
COPY default_redpanda_catalog=>example_table TO '/path/to/exampleexport.csv' WITH (NULL 'unknown');
```

In the exported file, `NULL` values are represented as `unknown`.

### [](#export-to-standard-output)Export to standard output

Send the data directly to the client instead of writing to a server-side file:

```sql
COPY default_redpanda_catalog=>book_inventory TO STDOUT;
```

The query returns:

```sql
"To Kill a Mockingbird",5
1984,8
"The Great Gatsby",3
"Moby Dick",2
"War and Peace",4
```

> 📝 **NOTE**
>
> -   Only CSV format is supported with `STDOUT`.
>
> -   The default delimiter for CSV format is a comma (`,`).

### [](#cloud-storage)Export to cloud storage

To export data to cloud storage, use the `COPY TO` command with the appropriate credentials option for your provider.

#### [](#aws-s3)AWS S3

-   `AWS_REGION`: AWS region associated with the storage service.

-   `AWS_KEY_ID`: Key identifier for authentication.

-   `AWS_PRIVATE_KEY`: Access key for authentication.

-   `ENDPOINT`: URL endpoint for the storage service.


```sql
COPY default_redpanda_catalog=>film TO 's3://your-bucket/file_name'
WITH (AWS_CRED(AWS_REGION 'us-west-1', AWS_KEY_ID 'key_id', AWS_PRIVATE_KEY 'access_key', ENDPOINT 's3.us-west-1.amazonaws.com'),
      FORMAT CSV, HEADER ON, NULL 'unknown');
```

#### [](#google-cloud-storage)Google Cloud Storage

Pass the path to your credentials JSON file:

```sql
COPY default_redpanda_catalog=>film TO 'gs://your-bucket/file_name' WITH (GCS_CRED('/path/to/credentials.json'));
```

If you cannot use a path to the credentials file, pass its contents as a string:

```sql
COPY default_redpanda_catalog=>project TO 'gs://your-bucket/project_file' WITH (GCS_CRED('<contents of the credentials.json file>'));
```

You can also use `AWS_CRED` with GCS by setting the endpoint:

```sql
COPY default_redpanda_catalog=>project TO 'gs://your-bucket/project_file'
WITH (AWS_CRED(AWS_REGION 'region1', AWS_KEY_ID 'key_id', AWS_PRIVATE_KEY 'access_key', ENDPOINT 'https://storage.googleapis.com'));
```

> 💡 **TIP**
>
> For Google Cloud Storage, use HMAC keys for authentication. See the [HMAC keys - Cloud Storage](https://cloud.google.com/storage/docs/authentication/hmackeys) page for details.

#### [](#azure-blob-storage)Azure Blob Storage

-   `TENANT_ID`: Tenant identifier representing your organization’s identity in Azure.

-   `CLIENT_ID`: Client identifier used for authentication.

-   `CLIENT_SECRET`: Secret identifier used as a password for authentication.


```sql
COPY default_redpanda_catalog=>taxi_data TO 'wasbs://container-name/your_blob'
WITH (AZURE_CRED(TENANT_ID 'your_tenant_id' CLIENT_ID 'your_client_id', CLIENT_SECRET 'your_client_secret'));
```