Skip to content

Database Setup

The ACME CSE uses a database to store resources and other runtime data. You have the choice between a memory-based database, a simple file-based database and a PostgreSQL database.

TinyDB File-Based

The default database is a simple but fast file-based database using the TinyDB library. By default, it requires no additional setup.

The database files are stored by default in the directory {baseDirectory}/data (which can be changed by a configuration setting). The filenames in that directory have unique filenames that are derived from the database type, Service Provider ID, and CSE ID. For example, the file for the resources of the CSE with ID id-in and Service Provider ID acme.example.com would be named resources-acme.example.com-id-in.json.

You enable the TinyDB file-based database by setting the databaseType setting in the [basic.config] section to tinydb:

Enable TinyDB as database
[basic.config]
databaseType=tinydb

Writing and Data Safety

The CSE keeps the TinyDB database in memory and writes changes to the database files in the background. A changed file is written after a short delay, which is set by the writeDelay setting (default: 1 second). Several changes within that time are written together.

A database file is never changed in place. The CSE first writes the new content to a temporary file (with the extension .tmp), and then replaces the database file with it. This has the following consequences:

  • Crash or power loss: each database file contains either the previous or the new version, but never a partly written one. Changes that were not written yet are lost, i.e. at most the changes of the last writeDelay seconds.
  • Shutdown: when the CSE is stopped normally, e.g. with the console's quit command, with Ctrl-C, or with a SIGTERM signal (as sent by kill, systemd, or docker stop), all pending changes are written before the CSE exits.
  • Disk space: while a database file is written, the old file and the temporary file exist at the same time. Make sure that there is enough free space for an additional copy of the largest database file, usually the resources-…json file.
  • Full disk: if a database file cannot be written, the existing file is not changed. The CSE logs an error, keeps the changes in memory, and retries every 10 seconds. When there is enough space again, the changes are written, and this is logged as well.

A temporary file that is left over from an interrupted write is incomplete. It is removed when the CSE starts.

Backups

When the CSE starts, it makes a backup of the database files in the backup directory. Optionally, it also makes backups periodically while it is running (setting backupInterval, in minutes).

Each backup is stored in its own sub-directory, which is named by the UTC time of the backup, for example:

Backup directory
data/backup/
    20261128T091502Z/
    20261129T091455Z/
    20261130T142530Z/
        resources-acme.example.com-id-in.json
        identifiers-acme.example.com-id-in.json
        ...

After a new backup was made, the oldest backups are removed, so that only the number of backups set by backupCount is kept (default: 3). Setting backupCount to 0 disables backups. Note that each backup is a full copy of the database, so the required disk space grows with the number of backups.

A backup is taken from the CSE's in-memory data, not copied from the database files. All files of a backup therefore belong to the same moment. However, a request that is processed at exactly that moment may only be partly contained in the backup, because a single request can change several database files.

A backup is first written to a directory with the extension .tmp, which is renamed when the backup is complete. Such a directory left over from an interrupted backup is incomplete. It is removed at the next backup.

Other files and directories in the backup directory, for example a backup made by an older version of the CSE, are not changed or removed. The CSE logs a warning about them at startup.

To restore a backup, stop the CSE and copy the files of a backup directory into the database directory, replacing the existing files.

TinyDB In-Memory

TinyDB also provides a memory-based database that might be useful for testing and development purposes, or if you want to start with a clean database each time you start the CSE.

You enable the in-memory database by setting the databaseType setting in the [basic.config] section to memory:

Enable in-memory database
[basic.config]
databaseType=memory

PostgreSQL

An alternative to the file-based database is to use a PostgreSQL database. This requires a running PostgreSQL server to which the CSE can connect. The PostgreSQL connection settings are configured in the acme.ini configuration file.

The following steps describe how to set up a PostgreSQL database for the ACME CSE:

  1. Optional: Install PostgreSQL on your system. You can download the installer from the PostgreSQL website.
  2. Create a new database and user for the CSE. It is recommended to use the CSE-ID as the database name and as the role name.
    For example, you can use the following commands to create a new database named id-in and a role named id-in with the password acme:
Create database and role
psql -c "CREATE DATABASE \"id-in\";"
psql -c "CREATE USER \"id-in\" WITH PASSWORD 'acme';"
psql -c "GRANT ALL PRIVILEGES ON DATABASE \"id-in\" TO \"id-in\";"
  1. If not done during the setup procedure above: Edit the acme.ini configuration file and the following settings under the [database.postgresql] section:

    PostgreSQL database settings
        [database.postgresql]
        password = acme
    

    All other settings are optional and can be left at their default values. The database and role settings are set to the CSE-ID by default. If you used different names for the database and role, you have to adjust these settings accordingly. Also the host and port settings are set to localhost and 5432 by default. If your PostgreSQL server is running on a different host or port, you have to adjust these settings as well.
    You also need to enable the PostgreSQL database by setting the databaseType setting in the [basic.config] section to postgresql:

    Enable postgreSQL database
        [basic.config]
        databaseType=postgresql
    
  2. Run the CSE.
    The database schema, tables and other structures are created automatically by the CSE when it starts and connects for the first time.

Disabling PostgreSQL Support

The PostgreSQL database binding uses the psycopg2 Python library to connect to the PostgreSQL server. It contains binary components that are loaded when the database binding plugin is loaded. Sometimes, these binary components are not available for certain platforms. In this case, one cannot use the PostgreSQL database binding at all.

In this case, you can disable the PostgreSQL database by setting the databaseType setting in the [basic.config] section to tinydb or memory:

Disable PostgreSQL database
[basic.config]
databaseType=tinydb