Skip to main content
In this Python quickstart we will learn how to:
  • Retrieve database credentials
  • Install the libSQL package
  • Connect to a Turso database
  • Execute a query using SQL
  • Sync changes to local database
1

Retrieve database credentials

You will need an existing database to continue. If you don’t have one, create one.Get the database URL:
turso db show --url <database-name>
Get the database authentication token:
turso db tokens create <database-name>
Assign credentials to the environment variables inside .env.
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
You will want to store these as environment variables.
2

Install

First begin by adding libSQL to your project:
pip install libsql
3

Connect

Then import the package:
import libsql
Now connect to your local or remote database using the libSQL connector:
url = os.getenv("TURSO_DATABASE_URL")
auth_token = os.getenv("TURSO_AUTH_TOKEN")

conn = libsql.connect("hello.db", sync_url=url, auth_token=auth_token)
conn.sync()
conn = libsql.connect("hello.db")
cur = conn.cursor()
4

Execute

You can execute SQL queries against your existing database as follows:
conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);")
conn.execute("INSERT INTO users(id) VALUES (10);")

print(conn.execute("select * from users").fetchall())
5

Sync

If you need to sync your local database with a remote Turso Cloud database (local reads and writes with push/pull to the cloud), use Turso Sync. Turso Sync is built on the Turso Database engine and provides true local-first sync with explicit push() and pull() operations.