Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,150 of 9,150 pages
About the help in other languages
All help pages

libSQL and Turso driver

This page is for administrators who want to keep a site's database on a libSQL server or in Turso's cloud, and for developers who work on the driver. From Exponential 6.0.15 the kernel reaches a libSQL server over its…

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/features/6.0/libsql-database-driver.md

On this page

libSQL and Turso driver

This page is for administrators who want to keep a site's database on a libSQL server or in Turso's cloud, and for developers who work on the driver. From Exponential 6.0.15 the kernel reaches a libSQL server over its HTTP API: expLibSQLDB (DatabaseImplementation=libsql) for a libSQL server of your own (sqld, the libsql-server container) and expTursoDB (turso) for Turso. No library and no PHP extension of its own: PHP's curl extension, or allow_url_fopen without it.

What it is. libSQL is a fork of SQLite: the same SQL, the same file format, the same answers, with a server (sqld) that serves a database over HTTP. The driver is therefore the SQLite driver (eZSQLite3DB) with another transport: the kernel's SQLite schema, the SQLite schema handler, the SQL helpers and every place in the kernel that treats SQLite in its own way treat a libSQL server the same, and databaseName() stays sqlite. What changes is where the database is: on a server, which several web servers can share, instead of a file next to one PHP.

What it is for. A site that wants SQLite's simplicity without its one-machine limit (several web servers on one database), a site whose database runs in Turso's cloud, and a data table connection to a libSQL database. Every statement is an HTTP request: a read costs about 2 ms instead of SQLite's tens of microseconds, and a write outside a transaction costs what the server takes to make it durable (measured below). Keep the view cache on.

Contents

  1. Requirements
  2. Installing a site on a libSQL server
  3. Settings
  4. How a statement reaches the server
  5. What differs from SQLite
  6. What works and what does not
  7. Embedded file mode and embedded replicas
  8. A data table connection to a libSQL database
  9. How it is built
  10. Tests

Requirements

WhatWhere it comes from
A libSQL server: sqld 0.22 or later (Hrana 3; older servers are spoken to in Hrana 2), or a Turso databasethe container ghcr.io/tursodatabase/libsql-server (tested: sqld 0.24.33, SQLite 3.47.0); Turso's dashboard or turso db create
PHP's curl extension (recommended: keep-alive), or allow_url_fopen=Onbundled with PHP on every distribution; the stream wrapper is the fallback
The SQLite the server is built on (or PHP's, in the embedded file mode): 3.33 or laterthe installer checks sqlite_version()

PHP's sqlite3 extension is not needed on the web server. The Packagist clients were looked at and not taken: the PHP clients of libSQL either load Turso's native library through FFI or a PHP extension written in Rust (neither is installable from a host's packages), or are HTTP clients that wrap the same three requests in a framework of their own. The protocol is small (one JSON request per statement), so the driver speaks it itself.

Installing a site on a libSQL server

Start a server, for example in a container (the data in a volume):

docker run -d --name sqld -p 127.0.0.1:8080:8080 -v sqld-data:/var/lib/sqld ghcr.io/tursodatabase/libsql-server:latest

Then:

php bin/php/install.php --db=libsql --db-host=127.0.0.1 --db-port=8080 --apply

The server holds one database (without namespaces), so --db-name is only the name the site's settings keep. With an auth token (sqld --auth-jwt-key-file, or Turso), give the token as the password: EXP_INSTALL_DB_PASSWORD=<token> or --db-password. Turso:

EXP_INSTALL_DB_PASSWORD="$(turso db tokens create exponential)" \
  php bin/php/install.php --db=turso --db-url=libsql://exponential-acme.turso.io --apply

--db-url replaces --db-host and --db-port (http://host:8080, https://host, libsql://<database>-<org>.turso.io), --db-tls=enabled|disabled|auto chooses https (auto: Turso, or port 443). The kickstarter takes Type=libsql (also sqld) or Type=turso, with URL, TLS, Mode and CAFile in [database_init]; --db-mode=file and --db-cafile are their exp:install options.

The web setup wizard lists libSQL, and Turso after it, when the web PHP has curl. Its database page asks for the mode (a server, or the embedded file mode), the server URL or name and port, TLS, an optional CA file and the auth token, or for the file mode a file name; the page checks them and then connects, and a refused connection is shown with the server's reason. The token field always comes empty: the token is kept between the pages only sealed (encrypted with a key in var/setup/secret.key), never in clear text in the page, and is written as the Password of the settings, as exp:install writes it (chapter 5). Measured: a whole site installed through the wizard, driven by Playwright, on a local sqld and in the file mode (below).

The installer empties the server's database first (DatabaseAction=remove), as it empties a SQLite file: every table, view, index and trigger is dropped.

Settings

settings/site.ini (the installer writes [LibSQLSettings] into the siteaccesses and settings/override):

[DatabaseSettings]
DatabaseImplementation=libsql     # turso for Turso; sqld is accepted too
Server=10.0.0.5
Port=8080
Database=exponential
Password=                         # the auth token, when the server wants one

[LibSQLSettings]
URL=                              # http(s)://host:port or libsql://host, replaces Server and Port
TLS=auto                          # enabled, disabled, auto (Turso, or port 443)
AuthToken=                        # instead of Password; else LIBSQL_AUTH_TOKEN (TURSO_AUTH_TOKEN) from the environment
Timeout=30                        # seconds per request (at least the statements' lock wait + 5)
ConnectAttempts=3                 # tries of the first connection
ConnectWait=200                   # ms before the second try, doubled after each failure
KeepStream=enabled                # one server stream for all statements (below)
Mode=remote                       # file: the embedded file mode (Database is a file in var/storage/sqlite3)
CAFile=                           # a PEM file that checks the server's certificate over https

[DatabaseSettings] SQLiteBusyTimeout, SQLiteTransactionWait and their command line pairs keep their meaning: how long a statement and a transaction's start may wait. SQLitePragmas[] is not sent: the server's settings are its own. UseSlaveServer is ignored (Turso's replicas answer at the same URL).

How a statement reaches the server

Each statement is one POST /v3/pipeline (Hrana 3 over HTTP; /v2/pipeline on a server without it) with the statement and a get_autocommit request, on a stream of the server, which is the server's connection to the database. The stream stays open from statement to statement (KeepStream=enabled): a new stream is a new connection of the server to its database, and that cost 18 of a read's 20 ms and 70 of a write's 76 ms (sqld 0.24 on loopback) before the stream was kept. A transaction runs on one stream from BEGIN IMMEDIATE to its end, as on one SQLite connection. get_autocommit tells the driver after every statement whether the server still has the transaction open, so a transaction the server ended (an error, its transaction timeout) is known at once.

The server lets an idle stream go after 10 seconds. A statement outside a transaction that finds its stream gone (HTTP 400 "invalid baton"), or whose request did not reach the server, is sent once more on a new stream; inside a transaction the transaction is lost, and the statement fails with that reason. close(), at the end of every request, closes the stream.

What differs from SQLite

SQLite (a file)libSQL (a server)
Who waits for the write lockthe writer gate (a lock file next to the database) and SQLite's busy handlerthe server; no lock file
A long write transaction while another writer waitsthe other waits (up to SQLiteTransactionWait)sqld ends the first after its transaction timeout (about 5 s): its next statement fails with "the libSQL server ended the transaction", and the kernel rolls it back
md5(), BIT_OR(), BIT_AND(), MOD()functions registered in PHPmd5() of a literal is worked out in PHP, of a column it is the server's md5() as hex; BIT_OR() and BIT_AND() are written as MAX()/MIN() of each bit; MOD() is the server's own (math functions built in)
Temporary tablesCREATE TEMPORARY TABLErefused by the server: createTempTable() makes an ordinary table of the caller's unique name, dropped by dropTempTable() or at the end of the connection
PRAGMAs (WAL, cache, mmap)set per connectionthe server's own; none sent
Several web serversno (one machine's file)yes: one database for all
BackupVACUUM INTO, a copy of the filethe server's: a copy of sqld's data directory, Turso's point-in-time restore

What works and what does not

Measured on 2026-11-27 with the tests below against sqld 0.24.33 (SQLite 3.47.0) in a container, PHP 8.5 with curl, on the database node of the project's cluster (12 CPUs, load 40 to 50 from other work during the runs), and a whole site installed with exp:install --db=libsql.

ArealibSQL (sqld)Notes
Kernel schema (153 tables) and clean dataworksthe kernel's SQLite schema through eZSQLiteSchema: 4,205 statements in 22 to 49 s
Install with exp:install / kickstarter, package and extensionsworksthe multisite package with its content and extensions: 16 steps, 0 errors, 0 warnings, 9 checks passed; 1,289 s (22 minutes) on a busy shared host, most of it in writes outside a transaction
Web setup wizardworksdriven by Playwright on sqld 0.24.33: 12 pages, the multisite package installed in 1,447 s (26 minutes in all), 0 errors, 9 checks passed; front page and admin login 200. The token was in no page and no log, only in the written settings (and their compiled cache)
Embedded file mode (Mode=file)worksthe same wizard run in the file mode: installed in 98 s (160 s in all), 0 errors, 9 checks passed; front page and admin login 200
Reading pages, the admin, signing inworksfront page, site and admin login answer 200 (PHP's built-in server); signing in as admin works. Every statement is a round trip, so keep the view cache on
Transactions, nested begin()/commit(), rollback, a failed statement inside oneworks
New ids (lastSerialID()), a key of two columnsworksthe server's rowid; MAX + 1 where SQLite numbers no column
Unicode text, long text (800 KB), BLOB, 64-bit integersworks
Duplicate keys, missing tables (error numbers)worksSQLite's result codes: 19, 1
Portable helpers: paging, upsert, group concat, unix timeworks
Temporary tablesworksas ordinary tables (above)
Several readers and writers at onceworksthe server queues the writers; a write transaction held longer than the server's transaction timeout while another waits is ended by the server
Several web servers on one databaseworksone URL for all; files still need a shared directory or a DFS backend
DFS cluster backend on libSQLdoes notno backend; use a shared directory, or a DFS backend on MySQL, PostgreSQL, Oracle, SQL Server or MongoDB
Migrations after the installworks4 migrations applied, 0 failed (the installer's run)
Upgrade captions (the dump before an upgrade)does not"no built-in dump for a libSQL server": back the server up with its own tools and run with --no-backup; a rollback says the same
Migration captions (the marker of the database's state)worksthe rows of every table, hashed
Stream expired, connection lost, outside a transactionworkssent again once
Without curl (allow_url_fopen)worksthe live tests run once over PHP's stream wrapper
A token the server refusesworksthe connection fails with "HTTP 401", the token never in the message
Turso's cloudnot testedthe same protocol over HTTPS with a token; no account was used. expTursoDB refuses to connect without a token
Embedded replicasdoes notsee below
Data table connection (engines libsql, turso)worksbelow
REST API's own SQLite handler (ezcDbHandlerSqlite)does notit opens a file; the REST API itself runs on the driver

Measured

WhatTime
connect (first statement on a new stream)67 ms
a row read by its key (stream kept)2.1 ms
a row written outside a transaction (the server commits each)76 to 101 ms
a row written inside one transaction2.3 to 6.7 ms
the kernel's schema and clean data (4,205 statements)22 to 49 s
a whole site with exp:install22 minutes (1,335 s, migrations included)
the front page, first request (empty caches, no OPcache, PHP's built-in server)26.9 s
the same page, view cache filled1.9 s
the admin login page3.3 s

A read costs a round trip; a write outside a transaction costs the server's commit, which sqld makes durable (WAL and its own replication log) before it answers. Writes inside a transaction cost a round trip each. The numbers come from a loaded host; an idle server answers faster, a server in another data centre slower by the network's round trip.

Embedded file mode and embedded replicas

  • The embedded file mode ([LibSQLSettings] Mode=file, exp:install --db=libsql --db-mode=file, or the wizard's choice): the database is a local file in var/storage/sqlite3, and expLibSQLDB is then the SQLite driver itself, over PHP's sqlite3, with no server and no HTTP. Turso is always remote.

  • A libSQL database file read by the SQLite driver: a file sqld writes (<data dir>/dbs/default/data) is an SQLite file, and DatabaseImplementation=sqlite opens it, as long as no libSQL-only feature (vector columns, random rowids) is in it, and never while sqld runs on it (its WAL and replication log are its own). Useful to take a copy of a server's database to a machine of its own; not a way to share one.

  • Embedded replicas (a local file that a libSQL client keeps in sync with the primary and writes through to it) are made by the libSQL client library, which PHP cannot load without Turso's native library (FFI) or a PHP extension of its own. The driver does not offer them.

A data table connection to a libSQL database

Setup > Data connections, engine libsql (or turso): the server and port (8080, 443), the database's name, the auth token as the connection's secret, and the options url (instead of the server and port; its host must be in datatable.ini [ConnectionSettings] AllowedHosts[]) and tls. Data tables made there are SQLite tables on the server: the schema planner, the describer and the table lists treat them as SQLite. Both engines are in Engines[] by default; turso is marked untested.

rqlite and Cloudflare D1

Two more SQLite services use the same driver code with their own transport. The SQL, the schema and databaseName() (sqlite) are the SQLite driver's; md5 of a literal and BIT_OR/BIT_AND are rewritten as for libSQL, and MySQL's MOD( a, b ) is written as ((a) % (b)).

  • rqlite (DatabaseImplementation=rqlite, expRqliteDB): distributed SQLite over Raft. Server and Port (4001) name any node of the cluster (a follower forwards writes to the leader), User and Password its basic auth, [RqliteSettings] Level the read consistency (weak by default). Every statement is one request to /db/request.
  • Cloudflare D1 (DatabaseImplementation=d1, expD1DB): Server is the account id, Database the database id, Password an API token with D1 edit permission (or CLOUDFLARE_API_TOKEN). Every statement is one request to Cloudflare's REST API (/raw), which is rate-limited per account.

Neither has interactive transactions. BEGIN, COMMIT and ROLLBACK are kept as a state in the transport only: every statement commits on its own as it runs, and a ROLLBACK cannot take back what ran before it (a warning is logged). A failed publish can therefore leave half of its rows behind. rqlite has no md5() of a column. Both are listed in the setup wizard and in kickstart.ini; the levels they reached are in the database verification results.

How it is built

FileWhat
lib/ezdb/classes/explibsqldb.phpexpLibSQLDB extends eZSQLite3DB: the connection, the functions rewritten, temporary tables, no writer gate
lib/ezdb/classes/exptursodb.phpexpTursoDB: the profile turso
lib/ezdb/classes/explibsqlconnection.phpexpLibSQLConnection: Hrana over HTTP with the part of PHP's SQLite3 class the SQLite driver uses (exec(), query(), querySingle(), lastErrorCode(), lastInsertRowID() ...); curl or the stream wrapper
lib/ezdb/classes/explibsqlresult.phpexpLibSQLResult: the rows, read as an SQLite3Result
lib/ezdb/classes/explibsqlprofile.phpexpLibSQLProfile: the profiles libsql and turso, the URL and the token from the settings

Hrana's values come back as PHP's SQLite3 gives them (integers as int, a 64-bit value beyond PHP's range as a string, floats, text, blobs as bytes, NULL); its errors as SQLite's result codes (SQLITE_CONSTRAINT 19, TRANSACTION_TIMEOUT 4) with SQLite's message.

Tests

SuiteWhatRun
libsqlthe transport, the profiles, the driver on a stand-in server (PHP's sqlite3 answering Hrana as sqld does), the data connectionphp vendor/bin/phpunit --testsuite libsql
libsql-liveagainst a server: the kernel schema, values, transactions, ids, errors, the functions, the helpers, temporary tables, two connections, an expired stream, a data table, the stream wrapperEXP_TEST_LIBSQL_URL=http://127.0.0.1:8080 php vendor/bin/phpunit --testsuite libsql-live (EXP_TEST_LIBSQL_TOKEN for a server with a token)

The GitHub job libsql runs both on sqld 0.24.33 (PHP 8.5 and 8.3) and, on PHP 8.5, installs a whole site on the server and fetches its front page and admin login.

See also: Choosing a database, Database support.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x