Zato database on PostgreSQL

How to create the PostgreSQL database for pub/sub, the audit log and the scheduler, including TLS connections.

The Zato database holds the runtime data of an environment - pub/sub topics, messages and subscriptions, the audit log, the run state of each scheduled job and the rules of the rule engine. By default, it is a SQLite database in the environment directory. This page shows how to prepare a PostgreSQL server for it instead. PostgreSQL 13 and later is supported.

All servers of an environment and its Dashboard use the same database, which the Zato_Database_* environment variables select. The configuration of the environment - its channels, connections and security definitions - is kept separately and does not move to PostgreSQL.

Creating the database and the role

As an administrative user, create a login role named zato with a password, and a UTF8 database of the same name owned by that role. The servers create the tables and indexes the first time they connect.

The free-text search of the audit log uses trigram indexes from the pg_trgm extension, which the servers enable in the database when they create the tables. The extension ships with PostgreSQL, and the owner of a database can enable it without superuser rights.

Now, point the servers and the Dashboard at the database:

export Zato_Database_Type=postgresql
export Zato_Database_Host=db.example.com
export Zato_Database_Port=5432
export Zato_Database_Username=zato
export Zato_Database_Password=my.secret.password
export Zato_Database_Name=zato

A transaction that a server commits is on disk before the call that wrote it returns.

Enabling SSL/TLS on the server

Point PostgreSQL at its certificate files in postgresql.conf:

ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
ssl_ca_file = 'ca.crt'

The key file must belong to the user PostgreSQL runs as and must not be readable by anyone else:

chown postgres:postgres /var/lib/postgresql/data/server.key
chmod 600 /var/lib/postgresql/data/server.key

To refuse all unencrypted TCP connections, use hostssl instead of host in pg_hba.conf:

pg_hba.conf
# TYPE  DATABASE  USER  ADDRESS     METHOD
hostssl zato      zato  0.0.0.0/0   scram-sha-256

To additionally require a client certificate from the role (mutual TLS):

pg_hba.conf
hostssl zato      zato  0.0.0.0/0   scram-sha-256  clientcert=verify-ca

Reload the PostgreSQL configuration afterwards.

Enabling SSL/TLS on the client side

With the server prepared, turn TLS on with Zato_Database_SSL and point Zato_Database_SSL_CA_File at the CA certificate that issued the certificate of the database server:

export Zato_Database_SSL=on
export Zato_Database_SSL_CA_File=/path/to/ca.crt

The server certificate and its hostname are verified by default. If the certificate of the database names another host, e.g. in a development environment, Zato_Database_SSL_Verify=off turns the verification off - the connection remains encrypted but the certificate is not checked.

When clientcert is in use, add the client certificate and its key:

export Zato_Database_SSL_Cert_File=/path/to/client.crt
export Zato_Database_SSL_Key_File=/path/to/client.key

All the variables of the database are described in the environment variable reference.

Learn more