﻿---
title: Set up a time series data stream
description: This page shows you how to manually set up a time series data stream (TSDS). Before you create a time series data stream, review Data streams in Elasticsearch...
url: https://www.elastic.co/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/set-up-tsds
products:
  - Elastic Documentation
  - Elasticsearch
applies_to:
  - Serverless Elasticsearch projects: Generally available
  - Serverless Observability projects: Generally available
  - Serverless Security projects: Generally available
  - Serverless Elasticsearch Vector Database projects: Unavailable
  - Elastic Stack: Generally available
---

# Set up a time series data stream
This page shows you how to manually set up a [time series data stream](https://www.elastic.co/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/time-series-data-stream-tsds) (TSDS).

## Before you begin

- Before you create a time series data stream, review [Data streams in Elasticsearch](https://www.elastic.co/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams) and [TSDS concepts](https://www.elastic.co/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/time-series-data-stream-tsds). You can also try the [quickstart](https://www.elastic.co/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/quickstart-tsds) for a hands-on introduction.
- Make sure you have the following permissions:
  - [Cluster privileges](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/5112/reference/elasticsearch/security-privileges#privileges-list-cluster)
    - `manage_index_templates` for creating a template to base the TSDS on
  - [Index privileges](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/5112/reference/elasticsearch/security-privileges#privileges-list-indices)
    - `create_doc` and `create_index` for creating or converting a TSDS
    - `manage` to [roll over](#convert-existing-data-stream-to-tsds) a TSDS
    - `auto_configure` if you choose to turn on past index creation

<note>
  If you're working with OpenTelemetry data, try the [OpenTelemetry quickstarts](https://www.elastic.co/elastic/docs-builder/docs/5112/solutions/observability/get-started/opentelemetry/quickstart).
</note>


## Set up a TSDS

<stepper>
  <step title="Create an index template">
    The structure of a time series data stream is defined by an index template.
    You can create the template with the [create or update index template](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-put-index-template) API.
    For example:
    ```console
    PUT _index_template/my-weather-sensor-index-template
    {
      "index_patterns": ["metrics-weather_sensors-*"], <1>
      "data_stream": { }, <2>
      "template": {
        "settings": {
          "index.mode": "time_series" <3>
        },
        "lifecycle": { <4>
          "enabled": true,
          "data_retention": "60d"
        },
        "mappings": { <5>
          "properties": {
            "sensor_id": {
              "type": "keyword",
              "time_series_dimension": true <6>
            },
            "location": {
              "type": "keyword",
              "time_series_dimension": true
            },
            "temperature": {
              "type": "half_float",
              "time_series_metric": "gauge" <7>
            },
            "humidity": {
              "type": "half_float",
              "time_series_metric": "gauge"
            },
            "@timestamp": { <8>
              "type": "date"
            }
          }
        }
      },
      "priority": 500, <9>
      "_meta": {
        "description": "Template for my weather sensor data"
      }
    }
    ```

    <important applies-to="Elastic Stack: Generally available from 9.0 to 9.5">
      Without lifecycle management enabled, a time series data stream can grow into very large indices that never roll over. This can lead to performance issues. Always configure lifecycle management for production deployments.
    </important>

    <dropdown title="Component templates (optional)">
      If you're using [component templates](/elastic/docs-builder/docs/5112/manage-data/data-store/templates#component-templates) with a time series data stream, check the following requirements:
      - Each component template is valid on its own
      - The `index.routing_path` setting and its referenced dimension fields are defined in the same component template
      - The `time_series_dimension` attribute is enabled for fields referenced in `index.routing_path`
    </dropdown>
  </step>

  <step title="Turn on past index creation (optional)">
    <applies-to>
      - Elastic Stack: Generally available since 9.5
    </applies-to>

    Elasticsearch can create missing backing indices when you add data that precedes existing time ranges.
    To enable this feature, update the cluster settings:
    ```console
    PUT _cluster/settings
    {
      "persistent": { <1>
        "data_stream.past_tsdb_index_creation_enabled": true,
        "data_streams.past_tsdb_index_interval": "2d" <1>
      }
    }
    ```

    1. These settings are persistent and apply to all data streams in the cluster.
    2. By default, each past backing index covers one day of data. Refer to [`data_streams.past_tsdb_index_interval`](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/5112/reference/elasticsearch/configuration-reference/miscellaneous-cluster-settings#time-series-data-stream).
  </step>

  <step title="Create the {{tsds}} and add data">
    You can create a time series data stream explicitly with the [create a data stream](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-create-data-stream) API.
    You must give it a name that matches the `index_patterns` in your index template.
    For example:
    ```console
    PUT _data_stream/metrics-weather-sensors
    ```
    Alternatively, you can create it implicitly by indexing a document.
    The TSDS is created automatically as long as the index name in your API matches the index template pattern.
    <important>
      To add data to your data stream with the following bulk API request or POST request, you must update the timestamps to within two hours of your current time.
      Data added to a TSDS must fit the [accepted time range](/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/time-bound-tsds#tsds-accepted-time-range).<applies-to>Elastic Stack: Generally available since 9.5</applies-to> If you turned on past index creation, Elasticsearch creates backing indices automatically as historical documents are added within the eligible write window. For more details, go to [How time-bound indices work](/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/time-bound-tsds#tsds-accepted-time-range).
    </important>

    ```console
    PUT metrics-weather-sensors/_bulk
    { "create":{ } }
    { "@timestamp": "2099-05-06T16:21:15.000Z", "sensor_id": "SENSOR-001", "location": "warehouse-A", "temperature": 26.7,"humidity": 49.9 }
    { "create":{ } }
    { "@timestamp": "2099-05-06T16:25:42.000Z", "sensor_id": "SENSOR-002", "location": "warehouse-B", "temperature": 32.4, "humidity": 88.9 }

    POST metrics-weather-sensors/_doc
    {
      "@timestamp": "2099-05-06T16:21:15.000Z",
      "sensor_id": "SENSOR-00002",
      "location": "warehouse-B",
      "temperature": 32.4,
      "humidity": 88.9
    }
    ```
    For more information about adding documents to data streams, go to [Use a data stream > Add documents to a data stream](/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/use-data-stream#add-documents-to-a-data-stream).
  </step>

  <step title="Verify setup">
    To make sure your time series data stream is working, try some GET requests.View data stream details:
    ```console
    GET _data_stream/metrics-weather-sensors
    ```
    Check the document count in a time series data stream:
    ```console
    GET metrics-weather-sensors/_count 
    ```
    Query the time series data:
    ```console
    GET metrics-weather-sensors/_search 
    {
      "size": 5,
      "sort": ["@timestamp"]
    }
    ```
  </step>
</stepper>


## Advanced setup


### Convert an existing data stream to a TSDS

You can convert an existing data stream to a TSDS only if it uses the `standard` [index mode](/elastic/docs-builder/docs/5112/manage-data/data-store/index-basics#index-mode). Converting a data stream that uses any other index mode, such as `logsdb`, is not supported.
To convert the data stream, follow these steps:
1. Update your existing index template and component templates (if any) to include time series settings.
2. Use the [rollover API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-rollover) to manually roll over the existing data stream's write index, to apply the template changes. For example:

```console
POST metrics-weather-sensors/_rollover
```

<note>
  After the rollover, new backing indices will have time series functionality. Existing backing indices are not affected by the rollover because their `index.mode` cannot be changed.
</note>


### Secure a time series data stream

To control access to a TSDS, use [index privileges](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/5112/reference/elasticsearch/security-privileges#privileges-list-indices). Privileges set on a TSDS also apply to the backing indices.
For an example, refer to [Data stream privileges](/elastic/docs-builder/docs/5112/deploy-manage/users-roles/cluster-or-deployment-auth/granting-privileges-for-data-streams-aliases#data-stream-privileges).
<applies-to>Elastic Stack: Generally available since 9.5</applies-to> Users who write documents that trigger creation of [past backing indices](/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/time-bound-tsds#tsds-past-index-creation) need the `auto_configure` index privilege on the data stream, in addition to privileges that allow indexing (such as `create_doc` or `index`).
Users with only the `index` privilege receive a `security_exception` when a write would create a past backing index.

## Next steps

Now that you've set up a time series data stream, you can manage and use it like a regular data stream. For more information, refer to:
- [Use a data stream](https://www.elastic.co/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/use-data-stream) for indexing and searching
- [Change data stream settings](/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/modify-data-stream#data-streams-change-mappings-and-settings) as needed
- Query time series data using the ES|QL [`TS` command](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/5112/reference/query-languages/esql/commands/ts)
- <applies-to>Elastic Stack: Generally available since 9.5</applies-to> [Load historical data into a TSDS](https://www.elastic.co/elastic/docs-builder/docs/5112/manage-data/data-store/data-streams/load-historical-tsds)
- Use [data stream APIs](https://www.elastic.co/docs/api/doc/elasticsearch/group/endpoint-data-stream)