# Keep a queryable mail DNS history in PostgreSQL

Store each observed change to MX, SPF or DMARC with the domain, timestamp and both configurations. A unique change ID makes replay safe.

[Use template](https://dashboard.fasthook.io/workflow/new?templateKey=cloudflare-dns-mail-audit-to-postgresql&templateName=Keep+a+queryable+mail+DNS+history+in+PostgreSQL)

**Required accounts**

- Cloudflare DNS
- PostgreSQL

## Keep a queryable mail DNS history in PostgreSQL

### 1. Trigger · Mail routing or authentication changed

**Cloudflare DNS**. Start when the observed MX, SPF or DMARC records differ from the previous check. The first check saves a baseline.

### 2. Action · Upsert the DNS audit row

**PostgreSQL**. Insert a row or update mapped columns when a unique key already exists. Use a stable key for repeatable delivery.

## Set up your workflow

1. **Create the audit table** Create a table with change\_id TEXT PRIMARY KEY, domain TEXT, previous TEXT, current TEXT and observed\_at TIMESTAMPTZ. Grant the PostgreSQL connection the permissions needed for an upsert.
2. **Set the observation scope** Select the Cloudflare zone and enter the full mail domain in the trigger. Its first successful poll saves a baseline. Map the database step to your audit table.
3. **Check replay behavior** Change a mail record on a test domain after the baseline. Compare the previous and current values in PostgreSQL. Replaying the same change should still leave one row for its change ID.

## Related integrations

- [Cloudflare DNS integrations](https://www.fasthook.io/apps/cloudflare-dns/integrations)
- [PostgreSQL integrations](https://www.fasthook.io/apps/postgresql/integrations)
