Skip to main content
Multilingual TursoDB, MVCC & AI Agent database guides Read Engine Guide →
Turso Database Fundamentals Sync

Usage

How to enable and use sync with Turso across TypeScript, Python, and Go.

This guide shows how to set up a Turso database and use the sync features from your application.

NOTE

This particular usage uses the Turso Cloud to sync the local Turso databases and assumes that you have an account.

1. 1. Setup Turso Cloud database

  • Follow our Quickstart to install the CLI, create a database

  • Get the database URL (turso://...):

turso db show <db>
  • Create a token for your app:
turso db tokens create <db>

2. 2. Setup a basic connection with sync

You need three essentials to enable sync:

  • Local path: where the local, synced tursodb file is stored
  • Remote URL: your Turso Cloud URL (turso://...)
  • Auth token: Turso Cloud token to authenticate requests
import { connect } from '@tursodatabase/sync';

const db = await connect({
  path: './app.db',                               // local path
  url: 'turso://...',                            // remote URL (generated with turso db show <db-name> --url)
  authToken: process.env.TURSO_AUTH_TOKEN,        // authentication token (generated with turso db tokens create <db-name>)
  // longPollTimeoutMs: 10_000,                   // optional: server waits before replying to pull
  // bootstrapIfEmpty: false,                     // set to false to avoid bootstrapping on first run
});
import os
import turso.sync

conn = turso.sync.connect(
    path="./app.db",                                  # local path
    remote_url="turso://...",                        # remote URL (generated with turso db show <db-name> --url)
    auth_token=os.environ["TURSO_AUTH_TOKEN"],        # authentication token (generated with turso db tokens create <db-name>)
    # long_poll_timeout_ms=10_000,                    # optional: server waits before replying to pull
    # bootstrap_if_empty=False,                       # set to false to avoid bootstrapping on first run
)
package main

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

db, err := turso.NewTursoSyncDb(ctx, turso.TursoSyncDbConfig{
  Path:              "./app.db",                    // local path
  RemoteUrl:         "turso://...",                // remote URL (generated with turso db show <db-name> --url)
  AuthToken:         os.Getenv("TURSO_AUTH_TOKEN"), // authentication token (generated with turso db tokens create <db-name>)
  // LongPollTimeoutMs: 10_000,                     // optional: server waits before replying to pull
  // BootstrapIfEmpty: new(bool),                   // *bool; point at false to avoid bootstrapping on first run
})
NOTE

On the first run, the local database is automatically bootstrapped from the remote — so the remote must be reachable during the initial connect.

If you set bootstrap_if_empty to false, the local database will start empty instead. You can bootstrap or update it later at any time by explicitly calling pull().

3. 3. Push changes

Push sends your local changes to the Turso Cloud server. Under the hood, logical statements are sent, and on conflicts the strategy is “last push wins”. COMMITTED WRITE transactions which overlap with WRITE transactions that are still open MAY be delayed. To push every committed transaction, make sure no write transaction is open when you push.

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

await db.push();
conn.execute("CREATE TABLE IF NOT EXISTS notes(id TEXT PRIMARY KEY, body TEXT)")
conn.commit()
conn.execute("INSERT INTO notes VALUES ('n1', 'hello')")
conn.commit()

conn.push()
// create *sql.DB instance
conn, err := db.Connect(ctx)
if err != nil {
  return err
}

_, err = conn.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS notes(id TEXT PRIMARY KEY, body TEXT)")
if err != nil {
  return err
}
_, err = conn.ExecContext(ctx, "INSERT INTO notes VALUES ('n1', 'hello')")
if err != nil {
  return err
}

if err := db.Push(ctx); err != nil {
	return err
}

4. 4. Pull changes

Pull fetches remote changes and applies them locally. It returns a boolean indicating whether anything changed.

  • Configure long_poll_timeout_ms/LongPollTimeoutMs if you want the server to wait for changes and avoid empty replies.
  • If you pushed earlier, a subsequent pull can still return that something changed due to server-side conflict resolution frames.
// Returns true if anything changed locally
const changed = await db.pull();
console.info('pulled changes:', changed);
changed = conn.pull()
print("pulled changes:", changed)
changed, err := db.Pull(ctx)
if err != nil {
	return err
}
log.Println("pulled changes:", changed)

5. 5. Checkpoint

Checkpoint compacts the local WAL to bound local disk usage while preserving sync state.

await db.checkpoint();
conn.checkpoint()
if err := db.Checkpoint(ctx); err != nil {
	return err
}

6. 6. Stats

Stats help you observe sync behavior and usage (WAL sizes, last push/pull times, network usage, revision, etc.).

const s = await db.stats();
console.info({
  cdcOperations: s.cdcOperations,
  mainWalSize: s.mainWalSize,
  revertWalSize: s.revertWalSize,
  networkReceivedBytes: s.networkReceivedBytes,
  networkSentBytes: s.networkSentBytes,
  lastPullUnixTime: s.lastPullUnixTime,
  lastPushUnixTime: s.lastPushUnixTime,
  revision: s.revision,
});
s = conn.stats()
print({
    "cdc_operations": s.cdc_operations,
    "main_wal_size": s.main_wal_size,
    "revert_wal_size": s.revert_wal_size,
    "network_received_bytes": s.network_received_bytes,
    "network_sent_bytes": s.network_sent_bytes,
    "last_pull_unix_time": s.last_pull_unix_time,
    "last_push_unix_time": s.last_push_unix_time,
    "revision": s.revision,
})
s, err := db.Stats(ctx)
if err != nil {
	return err
}
log.Printf("stats: cdc=%v, main=%d revert=%d rx=%d tx=%d pull=%d push=%d revision=%s",
  s.CdcOperations,
  s.MainWalSize,
  s.RevertWalSize,
  s.NetworkReceivedBytes,
  s.NetworkSentBytes,
  s.LastPullUnixTime,
  s.LastPushUnixTime,
  s.Revision,
)

Offline-first writes

If your app needs to accept writes without internet connectivity, write locally and call push() when the connection is available. All changes are safely stored in the local database file until they can be synced.

import { connect } from '@tursodatabase/sync';

// bootstrapIfEmpty: false lets the app start offline without
// needing to reach the remote on first launch
const db = await connect({
  path: './local.db',
  url: process.env.TURSO_URL!,
  authToken: process.env.TURSO_AUTH_TOKEN!,
  bootstrapIfEmpty: false,
});

async function syncWhenOnline(db) {
  try {
    await db.push();
  } catch (e) {
    // No connectivity — changes are safe in the local file
    // and will sync on the next push() call
  }
}

// On app launch, pull the latest state if online
try {
  await db.pull();
} catch (e) {
  // Offline — local data is still available for reads and writes
}

// On a timer or connectivity event
await syncWhenOnline(db);
import os
import turso.sync

# bootstrap_if_empty=False lets the app start offline without
# needing to reach the remote on first launch
conn = turso.sync.connect(
    path="./local.db",
    remote_url=os.environ["TURSO_URL"],
    auth_token=os.environ["TURSO_AUTH_TOKEN"],
    bootstrap_if_empty=False,
)

def sync_when_online(conn):
    try:
        conn.push()
    except Exception:
        # No connectivity — changes are safe in the local file
        # and will sync on the next push() call
        pass

# On app launch, pull the latest state if online
try:
    conn.pull()
except Exception:
    # Offline — local data is still available for reads and writes
    pass

# On a timer or connectivity event
sync_when_online(conn)
// BootstrapIfEmpty pointing at false lets the app start offline
// without needing to reach the remote on first launch
noBootstrap := false
db, err := turso.NewTursoSyncDb(ctx, turso.TursoSyncDbConfig{
	Path:             "./local.db",
	RemoteUrl:        os.Getenv("TURSO_URL"),
	AuthToken:        os.Getenv("TURSO_AUTH_TOKEN"),
	BootstrapIfEmpty: &noBootstrap,
})

func syncWhenOnline(ctx context.Context, db *turso.TursoSyncDb) {
	if err := db.Push(ctx); err != nil {
		// No connectivity — changes are safe in the local file
		// and will sync on the next Push() call
		log.Println("push deferred:", err)
	}
}

// On app launch, pull the latest state if online
if _, err := db.Pull(ctx); err != nil {
	// Offline — local data is still available for reads and writes
	log.Println("pull deferred:", err)
}

// On a timer or connectivity event
syncWhenOnline(ctx, db)

This is the modern equivalent of the offline: true flag from @libsql/client Embedded Replicas. With Turso Sync, all reads and writes happen locally by default — you control when to sync with explicit push() and pull() calls.

Synced from official Turso documentation ( tursodatabase/turso-docs · MIT License) Official Docs ↗
Search
Press Esc to close · Powered by Pagefind