Skip to content
Beau Barker edited this page Aug 5, 2025 · 21 revisions

🐰 Add RabbitMQ Service

compose.yaml

services:
  rabbitmq:
    image: rabbitmq:3
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "check_running"]
      interval: 5s
      timeout: 3s
      retries: 20

The healthcheck lets other services wait for readiness.

Postgres (Publishing to RabbitMQ)

📦 1. Install pg_amqp

Clone the repository:

git clone https://github.com/omniti-labs/pg_amqp postgres/pg_amqp

Install the extension:

postgres/Dockerfile

RUN apt-get update && apt-get install -y \
 build-essential \
 postgresql-server-dev-17

# pg_amqp - Used by api schema
COPY ./pg_amqp /pg_amqp
WORKDIR /pg_amqp
RUN make
RUN make install

WORKDIR /var/lib/postgresql

You may need to fix "implicit int" errors in pg_amqp, which were reported here, and fixed but not yet merged.

Build Postgres:

docker compose build postgres

📚 2. Load Required Extensions

Add this to a migration file:

postgres/migrations/01-extensions.sql

-- amqp extension for rabbitmq connection
create extension amqp;

⚠️ Do not wrap this file in a BEGIN/COMMIT block — create extension is non-transactional.

🏗 3. Publish a message

We'll send a message when a row is inserted into a table.

postgres/migrations/03-create_api_schema.sql

begin;

create table api.task (
  id bigserial primary key,
  name text not null
);

create function api.task_updated() returns void
language plpgsql as $$
begin
  perform amqp.publish(1, 'amq.topic', 'tasks', json_build_object('event', 'task_updated', 'command', command)::text);
end;
$$;

create trigger task_created
after insert on api.task
for each row execute procedure api.task_created();

commit;

▶️ 4. Run the Migrations

bin/postgres migrate
Clone this wiki locally