> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Export Data with Changefeeds

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

When you create an Enterprise changefeed, you can include the <InternalLink path="create-changefeed">`initial_scan = 'only'`</InternalLink> option to specify that the changefeed should only complete a table scan. The changefeed emits messages for the table scan and then the job completes with a `succeeded` status. As a result, you can create a changefeed with `initial_scan = 'only'` to <InternalLink path="export">`EXPORT`</InternalLink> data out of your database.

New in v23.1: You can also [schedule a changefeed](#create-a-scheduled-changefeed-to-export-filtered-data) to use a changefeed initial scan for exporting data on a regular cadence.

The benefits of using changefeeds for this use case instead of <InternalLink path="export">export</InternalLink>, include:

* Changefeeds are jobs, which can be <InternalLink path="pause-job">paused</InternalLink>, <InternalLink path="resume-job">resumed</InternalLink>, <InternalLink path="cancel-job">cancelled</InternalLink>, <InternalLink path="create-schedule-for-changefeed">scheduled</InternalLink>, and <InternalLink path="alter-changefeed">altered</InternalLink>.
* There is observability into a changefeed job using <InternalLink path="show-jobs#show-changefeed-jobs">`SHOW CHANGEFEED JOBS`</InternalLink> and the <InternalLink path="ui-cdc-dashboard">Changefeeds Dashboard</InternalLink> in the DB Console.
* Changefeed jobs have built-in <InternalLink path="how-does-an-enterprise-changefeed-work">checkpointing</InternalLink> and <InternalLink path="monitor-and-debug-changefeeds">retries</InternalLink>.
* <InternalLink path="changefeed-sinks">Changefeed sinks</InternalLink> provide additional endpoints for your data.
* You can use the <InternalLink path="create-changefeed">`format=csv`</InternalLink> option with `initial_scan= 'only'` to emit messages in CSV format.

Changefeeds emit the same CSV format as <InternalLink path="export">`EXPORT`</InternalLink>. In v22.1, changefeeds emitted CSV data that wrapped some values in single quotes, which were not wrapped when exporting data with the `EXPORT` statement.

## Message formats

By default, changefeeds emit messages in JSON format. You can use a different format by <InternalLink path="create-changefeed">creating a changefeed</InternalLink> with the <InternalLink path="create-changefeed">`format`</InternalLink> option and specifying one of the following:

* `json`
* `csv`
* `avro`
* `parquet` (in <InternalLink version="releases" path="cockroachdb-feature-availability">**Preview**</InternalLink>)

## Examples

### Export data with a changefeed

To create a changefeed that will only complete an initial scan of a table(s), run the following:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE CHANGEFEED FOR TABLE movr.users INTO '{scheme}://{host}:{port}?{query_parameters}' WITH initial_scan = 'only', format=csv;
```

Or, use <InternalLink path="cdc-queries">CDC queries</InternalLink> to filter the data that your changefeed emits:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE CHANGEFEED INTO '{scheme}://{host}:{port}?{query_parameters}'
  WITH initial_scan = 'only', format=csv AS SELECT name, city FROM movr.users;
```

The job will return a job ID once it has started. You can use `SHOW CHANGEFEED JOBS` to check on the status:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW CHANGEFEED JOB {job ID};
```

When the scan has completed you will find the output shows `succeeded` in the `status` field.

### Create a scheduled changefeed to export filtered data

This example creates a nightly export of some filtered table data with a <InternalLink path="create-schedule-for-changefeed">scheduled changefeed</InternalLink> that will run just after midnight every night. The changefeed uses <InternalLink path="cdc-queries">CDC queries</InternalLink> to query the table and filter the data it will send to the sink:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE SCHEDULE sf_skateboard FOR CHANGEFEED INTO 'external://cloud-sink' WITH format=csv
  AS SELECT current_location, id, type, status FROM vehicles
  WHERE city = 'san francisco' AND type = 'skateboard'
  RECURRING '1 0 * * *' WITH SCHEDULE OPTIONS on_execution_failure=retry, on_previous_running=start;
```

The <InternalLink path="create-schedule-for-changefeed#schedule-options">schedule options</InternalLink> control the schedule's behavior:

* If it runs into a failure, `on_execution_failure=retry` will ensure that the schedule retries the changefeed immediately.
* If the previous scheduled changefeed is still running, `on_previous_running=start` will start a new changefeed at the defined cadence.

## See also

* <InternalLink path="changefeed-messages">Changefeed Messages</InternalLink>
* <InternalLink path="create-changefeed">`CREATE CHANGEFEED`</InternalLink>
* <InternalLink path="create-schedule-for-changefeed">`CREATE SCHEDULE FOR CHANGEFEED`</InternalLink>
