10.2. Connecting to your database by using the stored systemd-creds passwords
You can use the systemd-creds tool to securely store your encrypted credentials, and use the secrets stored in systemd-creds to connect to the database of your choice: PostgreSQL, SQLite, or MySQL.
Prerequisites
- The command-line assistant.
- Access to the database configuration file.
Procedure
-
Access your database configuration file at
/etc/xdg/command-line-assistant/config.toml. Remove the
usernameandpasswordparameters from the[database]section, for example:[database] type = "postgresql" host = "localhost" port = "5432" database = "history"注意If you leave the username and password in the configuration file, these credentials take precedence over the
systemd-credstool.Generate encrypted credentials for your username or password. The following example uses
systemd-ask-passwordcommands. The name must follow the schema ofdatabase-usernameanddatabase-password, otherwise,claddoes not load the credentials properly.To generate an encrypted username, run the following command:
$ systemd-ask-password -n | ( echo "[Service]" && systemd-creds encrypt --name=<database_username> -p - - ) >/etc/systemd/system/clad.service.d/<username>.confTo generate an encrypted password, run the following command:
$ systemd-ask-password -n | ( echo "[Service]" && systemd-creds encrypt --name=<database_password> -p - - ) >/etc/systemd/system/clad.service.d/<password>.conf
After updating the database credentials, reload
systemdand restart thecladdaemon to apply the changes:$ sudo systemctl daemon-reload $ sudo systemctl restart clad