KitDevguides

KitDev / Guides / Database

Database (SQLite, MongoDB and PostgreSQL)

KitDev stores its data (bot users, warnings, gban, PM Permit, contacts, snips, sudo, AFK and your settings) in a database. By default it uses SQLite, with nothing to configure, in the data/ folder. If you prefer, you can use MongoDB (local or Atlas) or PostgreSQL (local, Supabase, Neon...), with a single variable in the .env.

It is tested with SQLite, with an in-memory simulated MongoDB and with a real PostgreSQL (local, with SSL and behind a pgbouncer in transaction mode). It has also been tested from Termux with a real Atlas, Supabase and Neon (free plans).

Which one to use

SQLite (default) MongoDB PostgreSQL
Setup None DATABASE_URL in the .env DATABASE_URL in the .env
Where the data is In data/, on your machine On the MongoDB server On the PostgreSQL server
If the server restarts or is wiped Kept only if data/ is kept Always kept Always kept
Ideal for VPS, Termux or your PC Heroku and similar (ephemeral disk), or when you want the data outside the server The same, if you prefer Supabase or Neon, or already have a PostgreSQL
Backups .backup and automatic backups .backup, Atlas's or mongodump .backup, your provider's or pg_dump

Turning on MongoDB

  1. The MongoDB client (pymongo) already comes in requirements.txt. If you installed KitDev by hand without that file, install it in KitDev's environment:
pip install pymongo
  1. Put the URI in the .env. With Atlas it looks like the first line; with a local MongoDB, like the second:
DATABASE_URL=mongodb+srv://usuario:clave@cluster0.xxxxx.mongodb.net/kitdev
DATABASE_URL=mongodb://localhost:27017/kitdev
  1. If you already used KitDev, move your data before starting.
  2. Start KitDev as usual.

The database name is the one at the end of the URI. If there is none, it is called kitdev; you can also set it with DATABASE_NAME. MONGO_URI works the same as DATABASE_URL. To go back to SQLite, remove the variable: your files in data/ are never deleted.

Atlas (free plan)

MongoDB Atlas has a free plan (M0) with 512 MB and up to 100 operations per second. KitDev uses much less.

  1. Create an account and an M0 cluster.
  2. In Database Access, create a user with a password and the Read and write to any database role: with a read-only role KitDev couldn't save anything.
  3. In Network Access (IP Access List), click Add IP Address, type your server's IP in Access List Entry (A) and click Confirm (B). If your IP changes (like on Heroku or Termux), type 0.0.0.0/0: Atlas warns that it allows all IPv4 addresses, so use a long password.
  4. On your cluster click Connect and choose to connect with a client library (the screen is titled Connecting with MongoDB Client Library). Keep Language: Python (C), copy the URI with the copy button (D) and replace <db_username> and <db_password> (E) with your user and password, removing the < and > too. If the password has characters like @, / or :, write them encoded (%40, %2F, %3A).
  5. Add the database name at the end of the URI (.../kitdev?...); if you don't, KitDev uses kitdev. You don't need to create the database or the collections in Atlas: KitDev creates them by itself.

On Termux (Android) the mongodb+srv:// URI also works: the system has no /etc/resolv.conf, which is what Python uses to resolve it, so KitDev uses Termux's DNS or, if there is none, the public 1.1.1.1 and 8.8.8.8.

This is what Atlas's panels look like; letters A and B mark step 3 and C to E mark step 4. They may change over time: if a name no longer matches, look for your cluster's connection string under Connect.

Atlas, IP Access List: the 0.0.0.0/0 entry (A) and Confirm (B)

Atlas, Connect: Language Python (C), copy button (D) and the db_username and db_password placeholders (E)

Turning on PostgreSQL

  1. The PostgreSQL client (pg8000) already comes in requirements.txt. It is written in pure Python, so it also installs on Termux. If you installed KitDev by hand without that file, install it in KitDev's environment:
pip install pg8000
  1. Put the URI in the .env. Both postgresql:// and postgres:// work:
DATABASE_URL=postgresql://usuario:clave@host:5432/nombre_de_la_base
DATABASE_URL=postgresql://usuario:clave@localhost:5432/kitdev
  1. If you already used KitDev, move your data before starting.
  2. Start KitDev as usual. It creates the tables by itself: there is no SQL to run. With a local PostgreSQL, create the empty database first (for example, createdb kitdev).

URI details:

  • The database is the one at the end of the URI; DATABASE_NAME is not used with PostgreSQL.
  • If the password has characters like @, /, :, # or ?, write them encoded (%40, %2F, %3A, %23, %3F).
  • ?sslmode= accepts disable, prefer (the default: uses SSL if the server offers it), require, verify-ca and verify-full (with sslrootcert=path if you need to point to the certificate). Supabase and Neon already require SSL; parameters that KitDev does not use (like channel_binding) are ignored.
  • KitDev uses a single connection. If the server closes it for inactivity, it reconnects by itself and repeats the operation.
  • It does not use named prepared statements, so it should also work through a pooler in transaction mode (tested with pgbouncer and Neon's; not with Supabase's Transaction pooler).
  • KitDev turns on row level security (RLS, with no policies) on its tables. On Supabase this keeps its REST API from exposing them with the anon key; KitDev's own connection, which owns the tables, keeps reading and writing them as usual. If you read those tables with another role, it will need to be able to bypass RLS.

Supabase and Neon (free plans)

Supabase (supabase.com):

  1. Create an account and a project; keep the database password you choose when creating it.
  2. At the top, click Connect. In the Direct Connection string tab (A), under Connection Method choose Session pooler (B) and under Type, URI (C). The direct connection (db.<project>.supabase.co) uses IPv6 on the free plan; if your server or your Termux only have IPv4, the Session pooler does work. The Transaction pooler (port 6543) should work too.
  3. Copy the URI and replace [YOUR-DATABASE-PASSWORD] (D) with your password, removing the brackets too: if your password is abc123, it becomes postgres.<project>:abc123@..., not :[abc123]@.... If the password has symbols, encode them. With the poolers, the user looks like postgres.<project>.
  4. The free plan gives 500 MB of database per project. Free projects are paused after a week of inactivity and you have to resume them by hand from the dashboard.

This is what Supabase's panel looks like; the letters A to D mark what steps 2 and 3 explain. It may change over time: if a name no longer matches, look for the Session pooler connection string under Connect.

Supabase, Connect: Direct Connection string tab (A), Session pooler (B) and Type URI (C)

Supabase: the Session pooler connection string with [YOUR-DATABASE-PASSWORD] (D)

Neon (neon.com):

  1. Create an account and a project.
  2. Click Connect. Choose the database and the role (A). The Connection pooling switch (B) is optional: both strings work (the pooler one has -pooler in the host). Click Show password (C) to see the password and copy the string (D), for example with Copy snippet (E); when you paste it, check that it carries your password and not asterisks. It ends in ?sslmode=require&channel_binding=require; KitDev accepts it as is.
  3. The free plan gives 1 GB per project. The server suspends after 5 minutes without activity and resumes on the next query; KitDev reconnects by itself.

This is what Neon's panel looks like; the letters A to E mark what step 2 explains. It may change over time: if a name no longer matches, look for the Postgres connection string under Connect.

Neon, Connect: database and role (A), Connection pooling (B), Show password (C), connection string (D) and Copy snippet (E)

Moving your data

With your current SQLite and the URI in the .env, run from the KitDev folder:

python -m kitdev.migrate --dry-runpython -m kitdev.migratepython -m kitdev.migrate --to "postgresql://usuario:clave@host:5432/postgres" --force
  • The first line only counts what would be moved, without writing anything.
  • The second moves all the tables and your settings (state.json) and checks at the end that no row is missing. It works the same for MongoDB and PostgreSQL, depending on the URI.
  • If the database already has data, it stops so it does not overwrite it with older data. With --force it merges them (you can repeat it without duplicating anything).
  • --to gives the URI instead of reading it from the .env; --db and --state change the source files.
  • It does not modify or delete your data/.

If you start with a remote database without moving your data and you have settings in data/state.json, KitDev starts without them and warns you in the logs: move your data and restart.

What changes with MongoDB or PostgreSQL

  • Settings: they also move to the database. They are read from memory and saved in the background; when KitDev closes, anything pending is saved.
  • Backups: .backup and .restore export and import the database and your settings in a .json.gz file (see Backup). The automatic backups are not used, because the remote database already keeps your data; the 💾 Backups panel explains it. Your provider's backups, mongodump or pg_dump also work. The files in data/ (addons and media) are still local and are not in the .json.gz.
  • Heroku: you no longer lose data on restart, without depending on backups. See the Heroku guide.
  • Speed: each query travels over the network (about 50 to 150 ms with a cloud service). What is used on every message (sudo, gban and AFK) is in memory, so you do not notice it.
  • On startup: if it cannot connect to the database, KitDev stops with a clear message instead of starting with empty data.

If something fails

What happens What to do
No pude conectar con MongoDB Check the URI, your connection and that your IP is allowed in Network Access
No pude conectar con PostgreSQL Check the URI, your connection and the sslmode. With Supabase from an IPv4 network, use the Session pooler
No replica set members found yet MongoDB did not answer. KitDev tests the network and tells you the likely cause: your IP is not allowed in Network Access (allow 0.0.0.0/0), the cluster is paused in Atlas or your network blocks port 27017 (try another network or a VPN)
cannot open /etc/resolv.conf Android and Termux don't have that file. Update KitDev: it now uses another DNS to resolve mongodb+srv://. If it still fails with a DNS error, check that your network allows DNS queries
falta pymongo Run pip install pymongo in KitDev's environment
falta pg8000 Run pip install pg8000 in KitDev's environment
bad auth or Authentication failed (MongoDB) Check the user and the password; if it has special characters, encode them
password authentication failed (PostgreSQL) Check the user and the password; if it has special characters, encode them. On Supabase with a pooler, the user is postgres.<project>
does not appear to be an IPv4 or IPv6 address You left the brackets [ ] around the password in the URI: remove them
Can't create a connection to host db.<project>.supabase.co Supabase's direct connection uses IPv6 and your network does not have it: use the Session pooler
DATABASE_URL debe empezar por... The URI is badly written; copy your provider's one whole
DATABASE_URL no es válida The port or the sslmode in the URI is not correct
Your settings or data show up empty You did not move them: run python -m kitdev.migrate and restart
You want to go back to SQLite Remove DATABASE_URL from the .env; your files in data/ are still there