Set Up PostgreSQL, PostgresML and Elasticsearch on Hetzner, Daniel Joye

Step-by-step guide to an audit-ready open source data platform on Hetzner EU servers: PostgreSQL with pgAudit, PostgresML, Elasticsearch, encryption and backups.

This guide sets up the open source stack from the audit-ready data platforms page on hosted Hetzner servers in EU data centers: PostgreSQL as the governed source of record, PostgresML for machine learning inside the database, and Elasticsearch for search and log analytics. Each step says why it matters for audit readiness and how to check it worked.

Commands target Debian 13 (trixie) and use placeholders such as 10.0.0.2 for private addresses. Package versions change; check each project's official documentation before running anything in production.

  1. Order servers in an EU location

    Why: Keeping servers in Hetzner's EU data centers keeps the data under EU law, including the GDPR.

    • Choose Falkenstein or Nuremberg (Germany) or Helsinki (Finland); do not pick a US or Singapore location
    • Create one server for PostgreSQL and PostgresML and a separate server for Elasticsearch, so a search outage cannot touch the source of record
    • Select Debian 13 (trixie), add your SSH key, and attach both servers to one Hetzner private network
    • Sign Hetzner's data processing agreement (DPA) in the account settings

    Check: Both servers show an EU location and a private IP address in the Hetzner console.

  2. Harden access before anything else

    Why: Least-privilege access starts at the network: only the people and systems that need a port can reach it.

    • Create a Hetzner Cloud Firewall that allows SSH only from your admin IP address and blocks everything else from the internet
    • Allow database (5432) and search (9200) traffic only on the private network
    • Turn off SSH password login and enable automatic security updates
    sudo apt update && sudo apt -y upgrade
    sudo apt -y install unattended-upgrades ufw
    sudo sed -i 's/^#\?PasswordAuthentication .*/PasswordAuthentication no/' /etc/ssh/sshd_config
    sudo systemctl restart ssh
    sudo ufw allow from <ADMIN_IP> to any port 22
    sudo ufw allow from 10.0.0.0/16 to any port 5432   # database server only
    sudo ufw enable

    Check: From outside the private network, ports 5432 and 9200 do not answer.

  3. Encrypt data at rest

    Why: Encryption with keys you hold keeps the hosting provider out of the data and supports deletion evidence.

    • Attach a Hetzner Volume for data and encrypt it with LUKS before formatting
    • Keep the passphrase in your own secrets manager, never on the server's disk
    sudo apt -y install cryptsetup
    sudo cryptsetup luksFormat /dev/disk/by-id/<VOLUME_ID>
    sudo cryptsetup open /dev/disk/by-id/<VOLUME_ID> data
    sudo mkfs.ext4 /dev/mapper/data
    sudo mkdir -p /srv/data && sudo mount /dev/mapper/data /srv/data

    Check: lsblk shows the data volume as type crypt.

  4. Install PostgreSQL with encrypted, audited connections

    Why: PostgreSQL is the governed source of record; every connection must be encrypted and every sensitive action logged.

    • Install PostgreSQL and the pgAudit extension
    • Listen only on the private IP, require TLS and scram-sha-256 passwords
    • Log role changes and writes with pgAudit
    sudo apt -y install postgresql postgresql-17-pgaudit
    # postgresql.conf
    listen_addresses = '10.0.0.2'
    ssl = on
    password_encryption = scram-sha-256
    shared_preload_libraries = 'pgaudit'
    pgaudit.log = 'role, ddl, write'
    # pg_hba.conf
    hostssl all all 10.0.0.0/16 scram-sha-256
    sudo systemctl restart postgresql
    sudo -u postgres psql -c "CREATE EXTENSION pgaudit;"

    Check: SHOW ssl; returns on, and a test CREATE ROLE appears in the PostgreSQL log with an AUDIT line.

  5. Model roles and row-level security

    Why: Access by role and purpose, enforced in the database, is what lets you show an auditor who could see what.

    • Create one owner role for the schema and separate read and write roles for applications and analysts
    • Turn on row-level security for tables with member, customer, or policyholder data
    • Never let applications connect as the postgres superuser
    CREATE ROLE app_owner NOLOGIN;
    CREATE ROLE analyst_read NOLOGIN;
    CREATE ROLE app_service LOGIN PASSWORD '<STRONG_PASSWORD>' IN ROLE analyst_read;
    ALTER TABLE members ENABLE ROW LEVEL SECURITY;
    CREATE POLICY members_by_region ON members
      FOR SELECT TO analyst_read
      USING (region = current_setting('app.region'));

    Check: Signed in as an analyst role, queries return only the rows that role's policy allows.

  6. Add PostgresML for in-database machine learning

    Why: Training and predicting inside PostgreSQL keeps governed data where its access controls and audit logs already apply.

    • Install the pgml extension following the PostgresML project's current official instructions (its Docker image or a source build)
    • Pin the exact version you install and record it, so model results can be reproduced later
    • Grant pgml functions only to the roles that train or score models
    CREATE EXTENSION IF NOT EXISTS pgml;
    SELECT pgml.version();

    Check: SELECT pgml.version(); returns the version you recorded.

  7. Back up with point-in-time recovery

    Why: Reproducible history means you can restore exactly what the data looked like on a given day.

    • Use pgBackRest for encrypted full and incremental backups with archived write-ahead logs
    • Store backups on a Hetzner Storage Box in the same EU region, separate from the database server
    • Set retention to match your records schedule, and run a test restore every month
    sudo apt -y install pgbackrest
    # /etc/pgbackrest/pgbackrest.conf
    [global]
    repo1-type=sftp
    repo1-cipher-type=aes-256-cbc
    repo1-retention-full=4
    [main]
    pg1-path=/var/lib/postgresql/17/main

    Check: A restore to a scratch server reproduces a known table count from a chosen date.

  8. Install Elasticsearch with security on

    Why: Elasticsearch carries search and the audit logs; it must be private, encrypted, and password-protected like the database.

    • Install from Elastic's official package repository; recent versions turn on TLS and passwords during install
    • Bind it to the private IP only and save the generated elastic password in your secrets manager
    • Create separate users with limited roles for indexing and for searching
    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg
    # add Elastic's apt repository for the current major version, then:
    sudo apt update && sudo apt -y install elasticsearch
    # /etc/elasticsearch/elasticsearch.yml
    network.host: 10.0.0.3
    sudo systemctl enable --now elasticsearch

    Check: curl to https://10.0.0.3:9200 without credentials is refused; with the indexing user it answers.

  9. Connect the stack and ship the logs

    Why: PostgreSQL stays the source of record; Elasticsearch holds searchable copies and the evidence trail.

    • Index records from PostgreSQL into Elasticsearch through a pipeline you version and log, never by hand
    • Send PostgreSQL and pgAudit logs to Elasticsearch so access to sensitive data is searchable
    • Set index lifecycle rules so each log class is kept only as long as its retention period

    Check: A test write in PostgreSQL appears in Elasticsearch with its audit log line.

  10. Build the views custom

    Why: A view built for the data and the people reading it beats a generic dashboard.

    • Choose from the open source charting libraries per project
    • Follow Dr. Edward Tufte (Yale) for the interface: high data-ink ratio, small multiples, honest scales, no chart junk
    • Follow Dr. BJ Fogg (Stanford) for the experience: one clear prompt, low effort, the next action obvious

    Check: A reader can answer the main question the view was built for in under a minute.

Setup questions

How do I set up PostgreSQL, PostgresML and Elasticsearch on Hetzner?

Order Debian servers in a Hetzner EU location on one private network, firewall everything except SSH from your admin IP, encrypt data volumes with LUKS, install PostgreSQL with TLS and pgAudit, add role-based access and row-level security, install the pgml extension, back up with pgBackRest to a Storage Box, and install Elasticsearch with security on and bound to the private network.

Does hosting on Hetzner keep data under EU law?

Choosing a Hetzner location in Germany or Finland keeps the servers in the EU, under EU law including the GDPR. Sign Hetzner's data processing agreement and encrypt data with keys you hold so the provider cannot read it.

Should PostgreSQL and Elasticsearch run on the same server?

No. Separate servers keep a search or log spike from slowing or endangering the source of record, and let each be firewalled and backed up on its own terms.

Audit-ready data platforms