> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Historical migrations overview Canonical URL: https://posthog.com/docs/migrate > **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.i.posthog.com` instead of `https://us.i.posthog.com` (ingestion host). > Prior to starting a historical data migration, ensure you do the following: > > 1. Create a project on our [US](https://us.posthog.com/signup) or [EU](https://eu.posthog.com/signup) Cloud. > 2. Sign up to a paid product analytics plan on [the billing page](https://us.posthog.com/organization/billing) (historic imports are free but this unlocks the necessary features). > 3. Set the `historical_migration` option to `true` when capturing events in the migration. This is automated if you are running a [managed migration](https://posthog.com/docs/migrate/managed-migrations.md). Historical migrations refer to ingesting and importing past data into PostHog for analysis. This includes: - Using [managed migrations](https://posthog.com/docs/migrate/managed-migrations.md) for automated data import from Mixpanel, Amplitude or S3 buckets. - Migrating from a different tool or platform like [Google Analytics](https://posthog.com/docs/migrate/google-analytics.md), [Heap](https://posthog.com/docs/migrate/migrate-from-amplitude.md), [Plausible](https://posthog.com/docs/migrate/plausible.md), or [LaunchDarkly](https://posthog.com/docs/migrate/launchdarkly.md). - Migrating from a [self-hosted PostHog instance to PostHog Cloud](https://posthog.com/docs/migrate/migrate-to-cloud.md). - Migrating from [one PostHog Cloud instance to another](https://posthog.com/docs/migrate/migrate-to-cloud.md#migrating-events-between-cloud-instances-eg-us---eu-cloud), for example US -> EU. - Adding past data from a third-party source into PostHog. > **What about exporting data from PostHog?** Use our [batch export feature](https://posthog.com/docs/data-pipelines/batch-exports.md) to export data from PostHog to external services like [S3](https://posthog.com/docs/data-pipelines/batch-exports/s3.md) or [BigQuery](https://posthog.com/docs/data-pipelines/batch-exports/bigquery.md), or a [file download export](https://posthog.com/docs/data-pipelines/file-download-exports.md) to export data as downloadable files. ## The basics of migrating data into PostHog Start your migration by formatting your data correctly. There is no way to selectively delete event data in PostHog, so getting this right is critical. This means: - **Using the correct event names.** For example, to capture a pageview event in PostHog, you capture a `$pageview` event. This might be different than the "name" other services use. - **Including the `timestamp` field.** This ensures your events are ingested with the correct time in PostHog. It needs to be in the ISO 8601 format and dated at least 48 hours before the time of import. - **Use the correct `distinct_id`.** This is the unique identifier for your user in PostHog. Every event needs one. For example, `posthog-js` automatically generates a `uuidv7` value for anonymous users. To capture events, you must use the PostHog Python SDK or the PostHog API `batch` endpoint with the `historical_migration` set to `true`. This ensures we handle this data correctly and you aren't charged standard ingestion fees for it. Using our Python or Node SDKs, you can capture events like this: ### Python ```python from posthog import Posthog from datetime import datetime posthog = Posthog( '', host='https://us.i.posthog.com', debug=True, historical_migration=True ) events = [ { "event": "batched_event_name", "distinct_id": "user_id", "timestamp": datetime.fromisoformat("2024-04-02T12:00:00+00:00"), "properties": {"account_type": "pro"} }, { "event": "batched_event_name", "distinct_id": "user_id", "timestamp": datetime.fromisoformat("2024-04-03T09:30:00+00:00"), "properties": {"account_type": "pro"} } ] for event in events: posthog.capture( event["event"], distinct_id=event["distinct_id"], properties=event["properties"], timestamp=event["timestamp"], ) posthog.shutdown() ``` ### Node.js ```javascript import { PostHog } from 'posthog-node' const client = new PostHog( '', { host: 'https://us.i.posthog.com', historicalMigration: true } ) client.debug() client.capture({ event: "batched_event_name", distinctId: "user_id", properties: {}, timestamp: new Date("2024-04-03T12:00:00Z") }) client.capture({ event: "batched_event_name", distinctId: "user_id", properties: {}, timestamp: new Date("2024-04-03T13:00:00Z") }) await client.shutdown() ``` An example `cURL` implementation using the `batch` API endpoint looks like this: ### Terminal ```bash curl -v -L --header "Content-Type: application/json" -d '{ "api_key": "", "historical_migration": true, "batch": [ { "event": "batched_event_name", "properties": { "distinct_id": "user_id" }, "timestamp": "2024-04-03T12:00:00Z" }, { "event": "batched_event_name", "properties": { "distinct_id": "user_id" }, "timestamp": "2024-04-03T12:00:00Z" } ] }' https://us.i.posthog.com/batch/ ``` ### Python ```python # The Python SDK also support historical migrations # See: /docs/libraries/python#historical-migrations import requests import json url = "https://us.i.posthog.com/batch/" headers = { "Content-Type": "application/json" } payload = { "api_key": "", "historical_migration": True, "batch": [ { "event": "batched_event_name", "properties": { "distinct_id": "user_id", }, "timestamp": "2024-04-03T12:00:00Z" }, { "event": "batched_event_name", "properties": { "distinct_id": "user_id" }, "timestamp": "2024-04-03T12:00:00Z" } ] } response = requests.post(url, headers=headers, data=json.dumps(payload)) print(response.text) ``` > Need someone to do the migration for you? A PostHog [forward-deployed engineer](https://posthog.com/services.md) can help! ## Best practices for migrations - We highly recommend testing at least a part of your migration on a test project before running it on your production project. - Separate exporting your data from your service from importing it into PostHog. Store it in a storage service like S3 or GCS in between to ensure exported data is complete. - Build resumability into your exports and imports, so you can just resume the process from the last successful point if any problems occur. For example, we use a cursor-based approach in our self-hosted migration tool. - To batch user updates, use the same request but with the `$identify` event. Same for groups and the `$group_identify` event.