Skip to main content

Need webhooks? Just use the Postgres HTTP extension

Reducing complexity

I previously wrote a blog about using pg_notify and a Golang listener to create a webhook based on Postgres events.

I no longer believe this is the best approach, after trying out the pgsql-http extension for this use case.

The http extension is super simple:

  1. Ensure the extension is installed and enabled.
  2. Create an SQL trigger for an event in your database.
  3. Call a function from your trigger that does a http call (GET, POST, etc possible).
  4. You can send a payload to an external webhook, including DB data, and even an X-API-Key header for authentication (or whatever you use in your API).

Now we no longer need an external service / listener running to wait for the events. Everything can be handled entirely in Postgres (a pattern that I am really loving recently, from cron jobs, webhooks, ML embeddings, etc).

How this works in practice

Adding the extension

FROM postgres:18 AS pg-18

RUN apt-get update \
    && apt-get install -y postgresql-18-http \
    && rm -rf /var/lib/apt/lists/*

For example: docker pull ghcr.io/hotosm/postgres:17-http

Adding a database trigger

PERFORM http((
    'POST',
    'https://some.url.com/endpoint',
    http_headers('Content-Type', 'application/json'),
    'application/json',
    webhook_payload::text
)::http_request);

For the example implementation, see this repo