All articles

Telegram bot SQLite: keep data across redeploys

Use /data on Deployka, verify your SQLite path, and update a bot without silently replacing its existing database with an empty one.

Why a bot sees an empty database after an update

A relative path such as bot.db depends on the process working directory. A database stored beside the code inside a container can disappear when that container is replaced. Use /data for persistent data in a Deployka container project. Keeping the volume through restart and redeploy is not an independent backup or a promise of indefinite retention after project deletion.

Python example

Read the persistent path from ENV in your code. Setting SQLITE_PATH=/data/bot.db only works if your application actually reads that variable.

import os
import sqlite3
from pathlib import Path

db_path = Path(os.getenv("SQLITE_PATH", "/data/bot.db"))
db_path.parent.mkdir(parents=True, exist_ok=True)
connection = sqlite3.connect(db_path)
connection.execute(
    "CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT)"
)
connection.commit()
connection.close()

SQLAlchemy uses a different URL format: an absolute path is typically sqlite:////data/bot.db. Do not pass that URL directly to sqlite3.connect, which expects a file path.

If you already have a database

  1. Do not delete the project or start with a new empty path. Identify the location of your existing database first.
  2. Create a consistent backup using SQLite facilities. Copying only the main file of an active WAL database can miss recent changes.
  3. Move the existing database into the volume separately from changing ENV. If you cannot access the file, contact support from the current project before redeploying.
  4. Check several known records after migration, then update the code.

Changing SQLITE_PATH does not move data automatically. A path-fix preview is not confirmation that your database has been migrated.

Verify the update

Check Logs for the actual path and for unable to open database file. Read a previously saved record in your bot, restart the project, and confirm that the record still exists. Never log BOT_TOKEN or publish your users' database contents.

If deployment fails

Check the directory, write permissions, and exact file name. A database is locked error needs investigation of concurrent writes and long transactions, not an automatic plan upgrade.

Replace ZIP in an existing project · Telegram bot with SQLite template · Support

Consistent backups: SQLite Backup API.