# Migrate to Turso

> Learn how to import your existing SQLite database to Turso.
> Canonical HTML: https://tursodb.cn/docs/cloud/migrate-to-turso/
> Upstream Source (MIT): https://docs.turso.tech/cloud/migrate-to-turso
> Language: en

---
This guide will walk you through the process of migrating your existing SQLite database to Turso Cloud. You can choose between using the Turso CLI, or the Platform API.

![Migrate to Turso](/images/turso-docs/cloud/import.png)

## Preparing to Migrate

Before importing your SQLite database to Turso, you need to ensure it's compatible with Turso's requirements. Specifically, your SQLite database should be using WAL (Write-Ahead Logging) mode.

**1. Open your SQLite database**

Use the SQLite command-line tool or any SQLite client to open your database:

```bash
sqlite3 path/to/your/database.db
```

**2. Set WAL journal mode**

Run the following command to switch your database to WAL mode:

```sql
PRAGMA journal_mode='wal';
```

This should return `wal` to confirm the change was successful.

**3. Checkpoint and truncate the WAL file**

Execute a checkpoint to ensure all changes are written to the main database file and truncate the WAL file:

```sql
PRAGMA wal_checkpoint(truncate);
```

**4. Verify the journal mode**

Confirm that your database is now in WAL mode:

```sql
PRAGMA journal_mode;
```

This should return `wal`.

**5. Close the database**

Exit the SQLite shell:

```sql
.exit
```

Your database is now ready for migration to Turso.

## Using the CLI

You can create a new database from a local SQLite file using the Turso CLI:

**1. Install the Turso CLI**

Make sure you have the [Turso CLI installed](/docs/cli/introduction/), and you're authenticated.

**2. Import your SQLite Database**

Import your existing SQLite database file using the `db import` command:

```bash
turso db import ~/path/to/my-database.db
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

Your database will be named after the file (without the .db extension), and
will import all your tables, data, and schema.

</div></div>


**3. Connect to your database**

You can now connect to your database using the shell:

```bash
turso db shell <database-name>
```

## Using the Platform API

You can also use the Platform API to migrate your existing SQLite database:

**1. Signup or Login using the Turso CLI**

Make sure to [install the Turso CLI](/docs/cli/installation/) if you haven't
already.

```bash Signup
turso auth signup
```

```bash Login
turso auth login
```

**2. Create a new Platform API Token**

Now create a new API Token using the Turso CLI:

```bash
turso auth api-tokens mint quickstart
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

Make sure to save the token somewhere safe. You'll need it to create a database and database token.

</div></div>


**3. Retrieve your account or organization slug**

The Platform API can be used with your personal account or with an organization. You'll need the obtain the `slug` of your account or organization using using the Turso CLI:

```bash
turso org list
```

**4. Create a Database for Import**

First, create a database that's ready to receive an import:

```bash
curl -X POST "https://api.turso.tech/v1/organizations/{organizationSlug}/databases" \
  -L \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
      "name": "new-database",
      "group": "default",
      "seed": { "type": "database_upload" }
  }'
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

The `seed` parameter with `"type": "database_upload"` indicates that you plan to upload a database file. If you don't proceed to upload a database, this database will count towards your quota, but will not be usable.

</div></div>


**5. Create a Database Token**

Generate an authentication token for your database:

```bash
curl -X POST "https://api.turso.tech/v1/organizations/{organizationSlug}/databases/{databaseName}/auth/tokens" \
  -L \
  -H "Authorization: Bearer TOKEN"
```

This token will be used to authenticate your upload request, and future requests to the database.

**6. Upload Your SQLite Database**

Finally, upload your SQLite database file:

```bash
curl -X POST "https://{databaseName}-{organizationSlug}.turso.io/v1/upload" \
  -H "Authorization: Bearer DATABASE_TOKEN" \
  --data-binary @/path/to/your/database.db
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

The `Authorization` header uses the database token you generated in the
previous step, not your Platform API token.

</div></div>


You're now ready to connect to your new Turso database using any of the Turso client libraries.

<div class="doc-cards">
<a class="doc-card" href="/docs/sdk/ts/quickstart/"><div class="doc-card-title"><span>TypeScript</span><span class="doc-card-arrow" aria-hidden="true">→</span></div></a>
<a class="doc-card" href="/docs/sdk/rust/quickstart/"><div class="doc-card-title"><span>Rust</span><span class="doc-card-arrow" aria-hidden="true">→</span></div></a>
<a class="doc-card" href="/docs/sdk/go/quickstart/"><div class="doc-card-title"><span>Go</span><span class="doc-card-arrow" aria-hidden="true">→</span></div></a>
<a class="doc-card" href="/docs/sdk/python/quickstart/"><div class="doc-card-title"><span>Python</span><span class="doc-card-arrow" aria-hidden="true">→</span></div></a>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>Android</span></div></div>
<a class="doc-card" href="/docs/sdk/swift/quickstart/"><div class="doc-card-title"><span>Swift</span><span class="doc-card-arrow" aria-hidden="true">→</span></div></a>
<a class="doc-card" href="/docs/sdk/php/quickstart/"><div class="doc-card-title"><span>PHP</span><span class="doc-card-arrow" aria-hidden="true">→</span></div></a>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>Ruby</span></div></div>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>Ruby on Rails</span></div></div>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>C</span></div></div>
</div>
<div class="doc-cards">

</div>
