This is a translation of the original English documentation page. Help us make it better.

2 PostgreSQL/TimescaleDB

Overview

This section contains best practices for setting up a PostgreSQL database in a secure way.

For an easy setup, it is recommended to follow the default PostgreSQL database creation instructions, which include creating the 'zabbix' user with full privileges on the Zabbix database. This user is the database owner that also has the necessary privileges for modifying the database structure when upgrading Zabbix.

To improve security, configuring a secure schema usage pattern, as well as creating additional database roles and users with minimal privileges is recommended. These roles and users should be configured based on the principle of least privilege, that is, they should only have privileges that are essential for performing the intended functions.

Database setup

Create the user that will be the database owner, and create the Zabbix database; the database owner is the user that is specified on database creation:

createuser -U postgres -h localhost --pwprompt usr_owner
       createdb -U postgres -h localhost -O usr_owner -E Unicode -T template0 zabbix

The following commands on this page must be executed while the connection to PostgreSQL is made specifically to the zabbix database.

Create the zabbix schema and set the database owner (usr_owner) to be the owner of this schema:

create schema zabbix authorization usr_owner;

The right to drop a database object or alter its definition is a privilege that is inherent to the database owner and that cannot be granted or revoked. Therefore, a clean install or upgrade has to be performed by the database owner.

Configure a secure schema usage pattern:

revoke create on schema public from public;
       revoke all on database zabbix from public;
       -- Note: search_path should point to the "zabbix" schema:
       alter role all in database zabbix set search_path = "zabbix";

After setting up the database, proceed to creating user roles.

Creating user roles

Create the following roles with the corresponding privileges:

  • zbx_srv - role for running Zabbix server and proxy:
create role zbx_srv;
       grant connect on database zabbix to zbx_srv;
       grant usage on schema zabbix to zbx_srv;
       alter default privileges for role usr_owner in schema zabbix grant delete, insert, select, update on tables to zbx_srv;
       alter default privileges for role usr_owner in schema zabbix grant select, update, usage on sequences to zbx_srv;
  • zbx_web - role for running Zabbix frontend and API:
create role zbx_web;
       grant connect on database zabbix to zbx_web;
       grant usage on schema zabbix to zbx_web;
       alter default privileges for role usr_owner in schema zabbix grant delete, insert, select, update on tables to zbx_web;
       alter default privileges for role usr_owner in schema zabbix grant select, update, usage on sequences to zbx_web;
  • zbx_bckp - role for table backup:
create role zbx_bckp;
       grant connect on database zabbix to zbx_bckp;
       grant usage on schema zabbix to zbx_bckp;
       alter default privileges for role usr_owner in schema zabbix grant select on tables to zbx_bckp;
       alter default privileges for role usr_owner in schema zabbix grant select, update, usage on sequences to zbx_bckp;

Table restoration is possible only by the database owner.

After creating roles, they can be assigned to users.

Assigning user roles

To assign the created user roles, create users and assign the relevant roles to them. Replace <user>, <role>, and <password> as necessary.

create user <user> with encrypted password '<password>';
       grant <role> to <user>;

For example, to create and assign the role for running Zabbix server and proxy:

create user usr_srv with encrypted password 'password';
       grant zbx_srv to usr_srv;

Database partitioning with TimescaleDB

Database partitioning is facilitated by TimescaleDB. To utilize TimescaleDB, Zabbix server requires database owner privileges.

If the PostgreSQL zabbix schema has already been created in the zabbix database, you can enable TimescaleDB with the following command:

echo "CREATE EXTENSION IF NOT EXISTS timescaledb WITH SCHEMA zabbix CASCADE;" | sudo -u postgres psql zabbix