> ## Documentation Index
> Fetch the complete documentation index at: https://abxbus.archivebox.io/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgresEventBridge

> Forward events using PostgreSQL LISTEN/NOTIFY plus table storage.

`PostgresEventBridge` stores event payloads in a Postgres table and uses `LISTEN/NOTIFY` for low-latency fanout.

## Go support

<Tabs>
  <Tab title="Go">
    `PostgresEventBridge` is not implemented in `abxbus-go` yet. Use [JSONLEventBridge](./bridge-jsonl) for the currently supported Go bridge.
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    // Not implemented in the Rust crate yet.
    // Use typed event JSON serialization with an external transport for now.
    ```
  </Tab>
</Tabs>

## Optional dependencies

<Tabs>
  <Tab title="Python">
    Install the Postgres extra (recommended):

    ```bash theme={null}
    pip install "abxbus[postgres]"
    ```

    Equivalent direct dependency install:

    ```bash theme={null}
    pip install asyncpg
    ```
  </Tab>

  <Tab title="TypeScript">
    Install the Postgres client package:

    ```bash theme={null}
    npm install abxbus pg
    ```

    This bridge is Node.js-only.
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    // PostgresEventBridge is not implemented in abxbus-go yet.
    // Use JSONLEventBridge for the currently supported Go bridge.
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    // Not implemented in the Rust crate yet.
    // Use typed events plus EventBus JSON serialization with an external transport for now.
    ```
  </Tab>
</Tabs>

## Constructor params

* `table_url`: `postgresql://user:pass@host:5432/dbname[/tablename]?...`
* `channel`: optional notify/listen channel (defaults to `abxbus_events`)
* `name`: optional bridge label

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from abxbus.bridges import PostgresEventBridge

    bridge = PostgresEventBridge(
        'postgresql://user:pass@localhost:5432/mydb/abxbus_events',
        channel='abxbus_events',
        name='PgBridge',
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```ts theme={null}
    import { PostgresEventBridge } from 'abxbus/bridges'

    const bridge = new PostgresEventBridge(
      'postgresql://user:pass@localhost:5432/mydb/abxbus_events',
      'abxbus_events',
      'PgBridge'
    )
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    // PostgresEventBridge is not implemented in abxbus-go yet.
    // Use JSONLEventBridge for Go process-to-process bridge tests and deployments.
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    // Not implemented in the Rust crate yet.
    // Use typed events plus EventBus JSON serialization with an external transport for now.
    ```
  </Tab>
</Tabs>

## Setup with a bus

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from abxbus import EventBus
    from abxbus.bridges import PostgresEventBridge

    bus = EventBus('AppBus')
    bridge = PostgresEventBridge('postgresql://user:pass@localhost:5432/mydb/abxbus_events')

    bus.on('*', bridge.emit)
    bridge.on('*', bus.emit)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```ts theme={null}
    import { EventBus } from 'abxbus'
    import { PostgresEventBridge } from 'abxbus/bridges'

    const bus = new EventBus('AppBus')
    const bridge = new PostgresEventBridge('postgresql://user:pass@localhost:5432/mydb/abxbus_events')

    bus.on('*', bridge.emit)
    bridge.on('*', bus.emit)
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    // PostgresEventBridge is not implemented in abxbus-go yet.
    // Use JSONLEventBridge for Go process-to-process bridge tests and deployments.
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    // Not implemented in the Rust crate yet.
    // Use typed events plus EventBus JSON serialization with an external transport for now.
    ```
  </Tab>
</Tabs>

## Behavior

* `emit(...)` upserts event payload data into the bridge table, then sends `NOTIFY` with the event id.
* `on(...)` registers inbound handlers and auto-starts listener startup.
* On notifications, the bridge fetches the full row payload, reconstructs an event, resets it, and emits locally.
* Columns are created on demand only for `event_*` fields.
* The full event JSON is stored in `event_payload` (not just non-`event_*` fields).
* `event_*` columns are also stored as flat mirrored fields for indexing/querying.
* Rehydration merges `event_payload` with `event_*` column values back into a flat event object.
* Runtime requirements: Python needs `asyncpg`, TypeScript needs `pg` and Node.js.
