Tutorial
An app with a database
A shared shopping list, with PostgreSQL in the same machine. The data stays from one deploy to the next, and while the app sleeps.
A shared flat, a shopping list: everyone adds what's missing and checks off what they bought. The list has to be the same for everyone, and survive every update of the app. So it needs a database.
What you say
Make a shared shopping list for my flat: we add an item, we check it off once it's bought. Keep the data in a PostgreSQL database, and use kazibo to put the app online.
What happens
- The agent writes the server, in Python here, and the list's page.
- In the app's configuration, it adds PostgreSQL, in the same machine. There's no service to create elsewhere and no password: the app talks to the database through a local file, and the database is never exposed to the internet.
- On start, the app creates its tables if they don't exist.
- Kazibo builds the machine, starts PostgreSQL then the app, and checks that the address answers.
Where the data lives
On the app's data disk. It survives deploys, restarts and sleep, and is backed up every night while the app runs.
Before every deploy, Kazibo copies the data. If the new version doesn't answer, it puts the previous one back with its data. If a version answers but damaged something, ask before you deploy again:
go back to the previous version, with its data
Looking into the database
how many items are on the list? show me the last ten
The agent runs the query in the app's machine and gives you the answer. No admin tool to install.
Going further
- Knowing who added what: add sign-in with Kazibo, then "show who added each item".
- A small app can also do with SQLite: the database file lives on the same data disk.
- Before a big change of structure, say "make a backup before you touch the database".
For the curious: the PostgreSQL configuration
The database and the app run in the same machine, joined by a local file, with no password:
{ lib, pkgs, ... }:
{
# The data is in /var/lib/postgresql/17, on the data disk.
services.postgresql = {
enable = true;
package = pkgs.postgresql_17;
ensureDatabases = [ "app" ];
ensureUsers = [
{
name = "app";
ensureDBOwnership = true;
}
];
# No network port: the database only opens to the machine itself.
settings.listen_addresses = lib.mkForce "";
};
systemd.services.app = {
wantedBy = [ "multi-user.target" ];
requires = [ "postgresql.target" ];
after = [ "postgresql.target" ];
environment.DATABASE_URL = "postgresql://app@/app?host=/run/postgresql";
serviceConfig = {
ExecStart = "${pkgs.python3.withPackages (p: [ p.psycopg ])}/bin/python3 ${./server.py}";
DynamicUser = true;
StateDirectory = "app";
Restart = "always";
};
};
system.stateVersion = "26.05";
}The server reads DATABASE_URL, like most PostgreSQL libraries.