Skip to content
Beau Barker edited this page Aug 3, 2025 · 38 revisions

Add a RabbitMQ Service

compose.yaml

services:
  rabbitmq:
    image: rabbitmq:3
    environment:
      RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS: "-rabbit channel_max 0"

Publish 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. Add Migrations

📚 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.

🏗 Use pg_amqp

Add to a migration file:

postgres/migrations/03-create_api_schema.sql

begin;

create table api.task (
  id serial 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_updated
before update on api.task
for each row execute procedure api.task_updated();

commit;

▶️ 3. Run the Migrations

bin/postgres migrate

Clone this wiki locally