This guide shows you how to set up Postgres change data capture (CDC) and stream changes to NATS using Sequin.

With Postgres data streaming to NATS, you can trigger workflows, keep services in sync, build audit logs, maintain caches, and more.

By the end of this how-to, you’ll have database changes flowing to a NATS subject.

Prerequisites

If you’re self-hosting Sequin, you’ll need:

  1. Sequin installed
  2. A database connected
  3. A NATS server ready to go

If you’re using Sequin Cloud, you’ll need:

  1. A Sequin Cloud account
  2. A database connected
  3. A NATS server ready to go

Basic setup

Prepare your NATS server

You’ll need a NATS server ready for Sequin to stream changes to. You can use either a local server for development or a cloud-hosted NATS service in production.

Local development with Docker

For local development with Sequin, you can quickly spin up a NATS server using Docker:

docker run --name nats -p 4222:4222 -d nats:latest

Create NATS sink

Navigate to the “Sinks” tab, click “Create Sink”, and select “NATS Sink”.

Configure the source

1

Select source table

Under “Source”, select the table you want to stream data from.

2

Choose message type

Specify whether you want to receive changes or rows from the table.

If you’re not sure which to choose, you can start with the default, “Changes”.

3

Specify filters

If you selected changes, in “Records to process”, you can indicate whether you want to receive insert, update, and/or delete changes.

You can also specify SQL filters to narrow down the events you want to receive. For example, if you only want to receive events for subscriptions that currently have an mrr greater than $100, you can add a filter on mrr > 100.

4

Specify backfill

You can optionally indicate if you want NATS to receive a backfill of all or a portion of the table’s existing data. Backfills are useful if you want to use NATS to process historical data.

You can backfill at any time. If you don’t want to backfill, toggle “Backfill” off.

5

Specify message grouping

Under “Message grouping”, you’ll most likely want to leave the default option selected to ensure events for the same row are sent to NATS in order.

Configure NATS

1

Enter NATS connection details

Fill in your NATS connection details:

  • Host (required): The hostname of your NATS server (e.g., localhost or nats.example.com)
  • Port (required): The port number (default: 4222)
2

Create the sink

Give your sink a name, then click “Create NATS Sink”.

Verify & debug

To verify that your NATS sink is working:

  1. Using the NATS CLI, subscribe to your subjects:
    nats sub "sequin.>"
    
  2. Make some changes in your source table
  3. Verify that the count of messages for your sink increases in the Sequin web console
  4. Verify that the messages appear in your NATS subscription

If messages don’t seem to be flowing:

  1. Click the “Messages” tab to view the state of messages for your sink
  2. Click any failed message
  3. Check the delivery logs for error details, including any NATS connection errors

Common issues to check:

  • NATS server is running and accessible
  • Host and port are correct
  • Network connectivity between Sequin and NATS

Next steps

  • Setup a processor

    Now that your Postgres data is flowing into NATS, you can setup a subscriber to read from the subjects and process the data. See the NATS documentation for details on consuming messages.

    Refer to the NATS sink reference for the shape of messages that Sequin will publish.

  • Deploy your implementation

    When you’re ready to deploy your implementation, see “How to deploy to production”.

  • Advanced configuration

    For more about how NATS sinks work, see the NATS sink reference.

Was this page helpful?