How to stream Postgres to Kafka
Receive Postgres changes in Kafka topics in real-time
This guide shows you how to set up Postgres change data capture (CDC) and stream changes to Kafka using Sequin.
With Postgres data streaming to Kafka, 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 Kafka topic.
Prerequisites
If you’re self-hosting Sequin, you’ll need:
- Sequin installed
- A database connected
- A Kafka cluster ready to go
If you’re using Sequin Cloud, you’ll need:
- A Sequin Cloud account
- A database connected
- A Kafka cluster ready to go
Basic setup
Prepare your Kafka cluster
You’ll need a Kafka cluster and topic ready for Sequin to stream changes to. You can use either a local cluster for development or a cloud-hosted Kafka service in production.
Local development with Docker
For local development with Sequin, you can quickly spin up a Kafka cluster using Docker Compose:
Start the cluster:
Create a topic for Sequin to publish to:
Production Kafka
For production environments, Sequin works with major managed Kafka providers as well as self-hosted Kafka clusters. To prepare your Kafka cluster for Sequin, you’ll need to:
- Create a dedicated topic for Sequin to publish to
- Create credentials (username/password) for Sequin to use
- Configure appropriate ACLs to allow Sequin to publish to your topic
Sequin currently supports SASL PLAIN, SCRAM-SHA-256, and SCRAM-SHA-512 authentication mechanisms with TLS encryption. Custom client certificates for verifying TLS connections are coming soon, let us know if you need them.
Once your cluster is ready, make note of:
- Bootstrap server addresses
- Topic name
- Authentication credentials
- Whether TLS is required
You’ll need these details when configuring your Kafka sink in the next section.
Create Kafka sink
Navigate to the “Sinks” tab, click “Create Sink”, and select “Kafka 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 to backfill of all or a portion of the table’s existing data into Kafka. Backfills are useful if you want to use Kafka to process historical data. For example, if you’re materializing a cache, you might want to warm it with existing rows.
You can backfill at any time. If you don’t want to backfill, toggle “Backfill” off.
Specify message grouping
DOCTODO: Do we let them specify a message key?
Under “Message grouping”, you’ll most likely want to leave the default option selected to ensure events for the same row are sent to Kafka in order.
Configure Kafka
Enter Kafka connection details
Fill in your Kafka connection details:
- Hosts (required): A comma-separated list of
host:port
pairs (e.g.,kafka1.example.com:9092,kafka2.example.com:9092
)- This can be either the full list of Kafka brokers or just the bootstrap server
- Topic (required): The Kafka topic to publish messages to (max 255 characters)
- Username: Username for SASL PLAIN authentication (optional)
- Password: Password for SASL PLAIN authentication (optional)
- TLS: Enable TLS/SSL encryption for the connection
Create the sink
Give your sink a name, then click “Create Kafka Sink”.
Verify & debug
To verify that your Kafka sink is working:
- Make some changes in your source table
- Verify that the count of messages for your sink increases in the Sequin web console
- Using your Kafka consumer tools, check your topic:
You should see the messages from Sequin appear in the topic.
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 Kafka connection errors
Next steps
-
Setup a processor
Now that your Postgres data is flowing into Kafka, you can setup a consumer group to read from the topic and process the data.
Refer to the Kafka sink reference for the shape of messages that Sequin will publish to Kafka.
-
Deploy your implementation
When you’re ready to deploy your implementation, see “How to deploy to production”.
-
Advanced configuration
For more about how Kafka sinks work, see the Kafka sink reference.