How to stream Postgres to GCP Pub/Sub
Receive Postgres changes via Google Cloud Pub/Sub in real-time
This guide shows you how to set up Postgres change data capture (CDC) and stream changes to Google Cloud Pub/Sub using Sequin.
With Postgres data streaming to Pub/Sub, 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 Pub/Sub topic.
Prerequisites
If you’re self-hosting Sequin, you’ll need:
- Sequin installed
- A database connected
- A Google Cloud project
If you’re using Sequin Cloud, you’ll need:
- A Sequin Cloud account
- A database connected
- A Google Cloud project
Basic setup
Enable Pub/Sub
In the Google Cloud console, navigate to Pub/Sub and ensure it’s enabled for your project.
Create a Pub/Sub topic
- Navigate to the Pub/Sub Topics page in your Google Cloud Console
- Click “CREATE TOPIC”
- Enter a topic ID (e.g., “postgres-changes”)
- (Optional) Enable “message retention” if you want to be able to replay messages
- Click “CREATE”
Make note of your:
- Google Cloud project ID (e.g., “my-project-123”)
- Pub/Sub topic ID (e.g., “postgres-changes”)
Create a service account
- Navigate to the IAM & Admin > Service Accounts page
- Select a project (if one is not already selected)
- Click “CREATE SERVICE ACCOUNT”
- Enter a name (e.g., “sequin-publisher”)
- Click “CREATE AND CONTINUE”
- Add the following two roles:
- “Pub/Sub Publisher” (
roles/pubsub.publisher
) - “Pub/Sub Viewer” (
roles/pubsub.viewer
)
- “Pub/Sub Publisher” (
- Click “CONTINUE”
- Click “DONE”
- Click on the newly created service account
- Go to the “KEYS” tab
- Click “ADD KEY” > “Create new key”
- Choose “JSON” format
- Click “CREATE”
The JSON key file will be downloaded to your computer. Keep this safe - you’ll need it when configuring the Pub/Sub sink in Sequin.
Create Pub/Sub sink
In Sequin, navigate to the “Sinks” tab, click “Create Sink”, and select “Pub/Sub 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 leave the default option “Changes” selected.
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 Pub/Sub to receive a backfill of all or a portion of the table’s existing data. Backfills are useful if you want to use Pub/Sub 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 Pub/Sub in order.
That way, if you’re using a Pub/Sub subscription with “message ordering” enabled, your system will receive events for a row in the same order they occurred in Postgres.
Configure Pub/Sub
Enter Pub/Sub details
Fill in your Pub/Sub connection details:
- Project ID (required): Your Google Cloud project ID (e.g., “my-project-123”)
- You can find this on the home page of the Google Cloud Console
- Topic ID (required): The Pub/Sub topic ID (e.g., “postgres-changes”)
- Service Account Credentials (required): Paste the entire contents of your service account JSON key file
Confirm connection
Click “Test Connection” to verify that Sequin can connect to your Pub/Sub topic.
Create the sink
Give your sink a name, then click “Create Pub/Sub Sink”.
Verify & debug
To verify that your Pub/Sub 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
- In the Google Cloud Console:
- Navigate to your Pub/Sub topic
- Click on the “MESSAGES” tab
- Under “Step 2”, select a Cloud Pub/Sub subscription
- Click “PULL” to see messages 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 Pub/Sub API errors
Common issues to check:
- Service account credentials are valid and not expired
- Service account has the “Pub/Sub Publisher” role
- Project ID and Topic ID match exactly
- Topic exists in the specified project
Next steps
-
Setup a processor
Now that your Postgres data is flowing into Pub/Sub, you can setup a subscriber to read from the topic and process the data. See the Pub/Sub documentation for details on consuming messages.
Refer to the Pub/Sub 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 Pub/Sub sinks work, see the Pub/Sub sink reference.
Was this page helpful?