How to stream Postgres to NATS
Build event-driven applications with NATS and Postgres change data capture (CDC). Stream database changes to NATS subjects for lightweight, scalable event distribution.
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 tables
Under “Source”, select the schemas and tables you want to stream data from.
Add filters (optional)
Add filters to the sink to control which database changes are sent to your NATS subject.
Add transform (optional)
Add a transform to the sink to modify the payload before it’s sent to NATS.
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.