How to stream Postgres to NATS
Receive Postgres changes in NATS subjects in real-time
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:
- Sequin installed
- A database connected
- A NATS server ready to go
If you’re using Sequin Cloud, you’ll need:
- A Sequin Cloud account
- A database connected
- 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:
Create NATS sink
Navigate to the “Sinks” tab, click “Create Sink”, and select “NATS Sink”.
Configure the source
Select source table
Under “Source”, select the table you want to stream data from.
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”.
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
.
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.
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
Enter NATS connection details
Fill in your NATS connection details:
- Host (required): The hostname of your NATS server (e.g.,
localhost
ornats.example.com
) - Port (required): The port number (default: 4222)
Create the sink
Give your sink a name, then click “Create NATS Sink”.
Verify & debug
To verify that your NATS sink is working:
- Using the NATS CLI, subscribe to your subjects:
- Make some changes in your source table
- Verify that the count of messages for your sink increases in the Sequin web console
- Verify that the messages appear in your NATS subscription
If messages don’t seem to be flowing:
- Click the “Messages” tab to view the state of messages for your sink
- Click any failed message
- 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?