# Lokaler Sync-Server

> Die Turso-CLI als lokalen Sync-Server für Entwicklung und Tests ohne Turso Cloud verwenden.
> Canonical HTML: https://tursodb.cn/de/docs/sync/local-sync-server/
> Upstream Source (MIT): https://docs.turso.tech/sync/local-sync-server
> Language: de

---
Die `tursodb`-CLI enthält einen integrierten Sync-Server, den Sie lokal ausführen können. So lassen sich Sync-Workflows vollständig auf dem eigenen Rechner entwickeln und testen — ohne Turso-Cloud-Konto.

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

Der lokale Sync-Server implementiert dasselbe Sync-Protokoll wie Turso Cloud. Ihr Anwendungscode funktioniert daher in beiden Umgebungen identisch.

</div></div>


## Server starten

Übergeben Sie `--sync-server` zusammen mit einer Adresse, um den Sync-Server zu starten. Das Datenbank-Argument legt fest, wo der Server seine Daten speichert:

```bash
tursodb ./server.db --sync-server 0.0.0.0:8080
```

Damit startet ein Sync-Server auf Port `8080` mit `./server.db` als Speicherdatei.

## Clients verbinden

Verbinden Sie Ihren Sync-Client mit `http://localhost:8080`. Für den lokalen Server ist kein Auth-Token erforderlich.

```ts TypeScript
import { connect } from '@tursodatabase/sync';

const db = await connect({
  path: './local-replica.db',
  url: 'http://localhost:8080',
});
```

```py Python
import turso.sync

conn = turso.sync.connect(
    path="./local-replica.db",
    remote_url="http://localhost:8080",
)
```

```go Go
package main

import (
	turso "turso.tech/database/tursogo"
)

db, err := turso.NewTursoSyncDb(ctx, turso.TursoSyncDbConfig{
    Path:      "./local-replica.db",
    RemoteUrl: "http://localhost:8080",
})
```

Nach dem Verbindungsaufbau funktionieren alle Sync-Operationen (`push`, `pull`, `checkpoint`, `stats`) exakt wie in der Anleitung zur [Verwendung](/de/docs/sync/usage/) beschrieben.

## Beispiel: Vollständiger lokaler Sync-Workflow

Dieses Beispiel zeigt den kompletten Ablauf — Server starten, Daten auf einem Client schreiben und auf einen zweiten Client synchronisieren.

**1. Sync-Server starten**

Öffnen Sie ein Terminal und starten Sie den Server:

```bash
tursodb ./server.db --sync-server 0.0.0.0:8080
```

**2. Von Client A schreiben und pushen**

```ts TypeScript
import { connect } from '@tursodatabase/sync';

const clientA = await connect({
  path: './client-a.db',
  url: 'http://localhost:8080',
});

await clientA.exec("CREATE TABLE IF NOT EXISTS notes (id TEXT PRIMARY KEY, body TEXT)");
await clientA.exec("INSERT INTO notes VALUES ('n1', 'hello from client A')");

await clientA.push();
```

```py Python
import turso.sync

client_a = turso.sync.connect(
    path="./client-a.db",
    remote_url="http://localhost:8080",
)

client_a.execute("CREATE TABLE IF NOT EXISTS notes (id TEXT PRIMARY KEY, body TEXT)")
client_a.commit()
client_a.execute("INSERT INTO notes VALUES ('n1', 'hello from client A')")
client_a.commit()

client_a.push()
```

```go Go
clientA, err := turso.NewTursoSyncDb(ctx, turso.TursoSyncDbConfig{
    Path:      "./client-a.db",
    RemoteUrl: "http://localhost:8080",
})
if err != nil {
    return err
}
connA, _ := clientA.Connect(ctx)

connA.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS notes (id TEXT PRIMARY KEY, body TEXT)")
connA.ExecContext(ctx, "INSERT INTO notes VALUES ('n1', 'hello from client A')")

clientA.Push(ctx)
```

**3. Von Client B pullen**

```ts TypeScript
import { connect } from '@tursodatabase/sync';

const clientB = await connect({
  path: './client-b.db',
  url: 'http://localhost:8080',
});

const changed = await clientB.pull();
console.log('changes pulled:', changed);

const rows = await clientB.query("SELECT * FROM notes");
console.log(rows);
// [{ id: 'n1', body: 'hello from client A' }]
```

```py Python
import turso.sync

client_b = turso.sync.connect(
    path="./client-b.db",
    remote_url="http://localhost:8080",
)

changed = client_b.pull()
print("changes pulled:", changed)

rows = client_b.execute("SELECT * FROM notes").fetchall()
print(rows)
# [('n1', 'hello from client A')]
```

```go Go
clientB, err := turso.NewTursoSyncDb(ctx, turso.TursoSyncDbConfig{
    Path:      "./client-b.db",
    RemoteUrl: "http://localhost:8080",
})
if err != nil {
    return err
}

changed, _ := clientB.Pull(ctx)
log.Println("changes pulled:", changed)

connB, _ := clientB.Connect(ctx)
rows, _ := connB.QueryContext(ctx, "SELECT * FROM notes")
```
