# 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](https://zato.io/docs/eda/index.html) topics, messages and subscriptions, the [audit log](https://zato.io/docs/admin/audit-log/index.html), the run state of each [scheduled job](https://zato.io/docs/dev/examples/scheduler.html) 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](https://zato.io/tutorials/devops/env-variables.html#zato-database) 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 {#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:

```bash
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 {#enabling-ssltls-on-the-server}

Point PostgreSQL at its certificate files in `postgresql.conf`:

```ini
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:

```bash
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

```text
# 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

```text
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 {#enabling-ssltls-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:

```bash
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:

```bash
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](https://zato.io/tutorials/devops/env-variables.html#zato-database).

## Learn more {#learn-more}

- [Production best practices](https://zato.io/docs/admin/production-best-practices.html) - What to configure before an environment takes real traffic
- [Audit log](https://zato.io/docs/admin/audit-log/) - Every request, response and error, and what you can do with them afterwards
- [Health checks](https://zato.io/docs/admin/health-checks.html) - Have Zato ping your outgoing connections instead of waiting for traffic to fail
- [Logging](https://zato.io/docs/admin/logging.html) - What is written where, and how to ship it to Splunk, Loki, Elastic or Datadog
- [GitOps with enmasse](https://zato.io/docs/admin/enmasse.html) - Keep the whole environment in git as YAML and import it anywhere
