This document describes how to create a Cloud SQL for PostgreSQL Source connector for Kafka Connect.
A Cloud SQL for PostgreSQL Source connector is an instance of a Debezium PostgreSQL connector. It reads row-level changes from a Cloud SQL for PostgreSQL database and writes them to topics in a Managed Service for Apache Kafka cluster.
Use cases for this connector include:
- Monitor row-level database changes in real time.
- Integrate database change events into an event-driven architecture.
- Respond to database events such as row insertions or deletions.
- Copy database changes to other systems.
Before you begin
Before creating a Cloud SQL for PostgreSQL Source connector, ensure you have the following:
A Cloud SQL for PostgreSQL instance with a database. To learn how to create these resources, see Create and query a Cloud SQL for PostgreSQL database using the Google Cloud console.
Required roles and permissions
To get the permissions that
you need to create a connector,
ask your administrator to grant you the
Managed Kafka Connector Editor (roles/managedkafka.connectorEditor) IAM role on your project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to create a connector. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to create a connector:
-
Create a connector:
managedkafka.connectors.create
You might also be able to get these permissions with custom roles or other predefined roles.
Grant permissions to read from Cloud SQL
The Managed Kafka service account must have permission to access Cloud SQL for PostgreSQL. Grant the following IAM roles to the service account:
- Cloud SQL Client (
roles/cloudsql.client) - Cloud SQL Instance User (
roles/cloudsql.instanceUser)
The Managed Kafka service account has the following format:
service-PROJECT_NUMBER@gcp-sa-managedkafka.iam.gserviceaccount.com,
where PROJECT_NUMBER is the project number of the Connect cluster.
If your Connect cluster is in a different project from your Managed Service for Apache Kafka cluster, see Create a Connect cluster in a different project.
Configure the database
Before you create the connector, you must configure database replication and enable the connector to authenticate with the database. The following sections describe these steps.
Enable logical decoding
A Cloud SQL for PostgreSQL Source connector relies on the logical decoding feature of PostgreSQL. To enable logical decoding in your Cloud SQL for PostgreSQL instance, perform the following steps.
Console
Go to Cloud SQL > Instances.
Click the name of the instance.
Click Edit.
Expand Flags and parameters.
Click Add a database flag.
In the Choose a flag list, select
cloudsql.logical_decoding.For Value, select
On.Click Done.
Click Save.
For more information, see Set up logical replication and decoding.
Configure change data capture (CDC)
After you enable logical decoding in your instance, enable change data capture (CDC) for the tables that you want to replicate.
To enable CDC for a table, run the CREATE PUBLICATION SQL statement. This
statement creates a publication, which defines a group of tables to replicate.
Option 1. Create a publication that replicates changes for all tables in the database.
CREATE PUBLICATION dbz_publication FOR ALL TABLES;Option 2. Create a publication for a specific set of tables.
CREATE PUBLICATION dbz_publication FOR TABLE TABLE_LIST;Replace
TABLE_LISTwith a comma-separated list of tables, in the format"schema_name"."table_name". Enclosing the schema and table names in double quotes, as shown, prevents syntax errors if the names contain special characters or uppercase letters.
By default, the connector uses dbz_publication for the publication name. To
use a publication with a different name, see
Publication name.
Create a user account for the Managed Kafka service account
The Cloud SQL for PostgreSQL Source connector uses IAM database authentication to connect to the database. To enable IAM database authentication, add the Managed Kafka service account to the Cloud SQL instance, as follows:
Console
Go to Cloud SQL > Instances
Click the name of the instance.
In the navigation pane, click Users.
Click Add user account.
In the Add a user account pane, select Cloud IAM.
In the IAM principal field, enter the following:
service-PROJECT_NUMBER@gcp-sa-managedkafka.iam.gserviceaccount.comReplace
PROJECT_NUMBERwith the project number of the Connect cluster.Click Add.
gcloud
Run the gcloud sql users create
command:
gcloud sql users create service-PROJECT_NUMBER@gcp-sa-managedkafka.iam \
--instance=INSTANCE_NAME \
--type=cloud_iam_service_account
Replace the following:
PROJECT_NUMBER: The project number of the Connect cluster.INSTANCE_NAME: The name of the Cloud SQL for PostgreSQL instance.
Due to the length limit on a database username, the
.gserviceaccount.com suffix is dropped from the
username, so the username is
service-PROJECT_NUMBER@gcp-sa-managedkafka.iam. When you
run SQL queries that reference the IAM user account, specify the
truncated name.
Configure the user account
After you create the IAM user account, connect to the database
as a user with the cloudsqlsuperuser role (such as the default postgres
user), and run the following SQL queries.
Console
Enable the user to read the write-ahead log.
ALTER USER "service-PROJECT_NUMBER@gcp-sa-managedkafka.iam" WITH REPLICATION;Grant the user
SELECTpermission on the tables.GRANT SELECT ON ALL TABLES IN SCHEMA "SCHEMA_NAME" TO "service-PROJECT_NUMBER@gcp-sa-managedkafka.iam";Alternatively, you can grant
SELECTpermission on individual tables. If you choose this option, you must also set the connector'stable.include.listconfiguration property to the list of allowed tables. The following SQL query grantsSELECTpermission on a single table:GRANT SELECT ON TABLE SCHEMA_NAME.TABLE_NAME TO "service-PROJECT_NUMBER@gcp-sa-managedkafka.iam";For each table, give the user access the table's schema. You can skip this step if the table is in the default
publicschema.GRANT USAGE ON SCHEMA SCHEMA_NAME TO "service-PROJECT_NUMBER@gcp-sa-managedkafka.iam";
Configure networking
A Cloud SQL for PostgreSQL Source connector can connect to the Cloud SQL instance in the following ways:
- Private IP
- Private Service Connect
- Public IP
For more information about these options, see Choose how to connect to Cloud SQL. As a security best practice, it's recommended to use either private IP or Private Service Connect, because these options don't require connecting to an external IP address.
The following table shows the network requirements for each option:
| IP address type | Requirements |
|---|---|
| Private IP | Configure private IP for your instance. For more information, see Configure private IP. |
| Private Service Connect |
|
| Public IP |
|
Create a Cloud SQL for PostgreSQL Source connector
To create a Cloud SQL for PostgreSQL Source connector, perform the following steps.
When the connector is initialized, it performs the following actions:
- Creates an initial snapshot of the database.
- Creates a Kafka topic for every table that has rows.
- For each database row, sends a change event to the corresponding topic.
While the connector is running, it continues to send change events to the topics. For more information about the initial snapshot, see Snapshots in the Debezium documentation.
Console
In the Google Cloud console, go to the Connect Clusters page.
Click the Connect cluster where you want to create the connector.
Click Create connector.
For the connector name, enter a string.
For guidelines on how to name a connector, see Guidelines to name a Managed Service for Apache Kafka resource.
For Connector plugin, select Cloud SQL for PostgreSQL Source.
In the Instance list, select the Cloud SQL instance.
In the Database list, select the Cloud SQL database.
In the Topic prefix field, enter a prefix to use for the Kafka topic names. Choose a unique prefix for each Cloud SQL for PostgreSQL Source connector.
Optional: In the Table names field, enter a comma-separated list of tables to read change data from, in the format
"schema_name"."table_name". If you leave this field empty, the connector reads change data from all non-system tables in the database.Optional: In the Configurations box, add configuration properties or edit the default properties. For more information, see Configure the connector.
You might need to override the defaults for the following properties:
driver.ipTypes: This property must match the network configuration of your Cloud SQL instance. See IP address types.slot.name: If you create multiple instances of the connector for the same database, specify a unique value for each connector. See Replication slots.
Optional: Select the Task restart policy. For more information, see Task restart policy.
Click Create.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Run the
gcloud managed-kafka connectors createcommand:gcloud managed-kafka connectors create CONNECTOR_ID \ --location=LOCATION \ --connect-cluster=CONNECT_CLUSTER_ID \ --config-file=CONFIG_FILEReplace the following:
CONNECTOR_ID: The ID or name of the connector. For guidelines on how to name a connector, see Guidelines to name a Managed Service for Apache Kafka resource. The name of a connector is immutable.LOCATION: The location where you create the connector. This must be the same location where you created the Connect cluster.CONNECT_CLUSTER_ID: The ID of the Connect cluster where the connector is created.CONFIG_FILE: The path to the YAML configuration file for the connector.
Here is an example of a configuration file for the Cloud SQL for PostgreSQL Source connector:
connector.class: io.debezium.connector.postgresql.PostgresConnector database.dbname: DATABASE_NAME driver.cloudSqlInstance: INSTANCE_ID driver.enableIamAuth: "true" driver.ipTypes: IP_TYPES driver.sslmode: disable key.converter: org.apache.kafka.connect.json.JsonConverter key.converter.schemas.enable: "false" plugin.name: pgoutput slot.name: SLOT_NAME table.include.list: TABLE_LIST topic.prefix: TOPIC_PREFIX value.converter: org.apache.kafka.connect.json.JsonConverter value.converter.schemas.enable: "true"Replace the following:
INSTANCE_ID: The ID of the Cloud SQL instance that contains the database, formatted as follows:PROJECT_ID:REGION:INSTANCE_NAME
DATABASE_NAME: The name of the Cloud SQL database to read from.IP_TYPES: A comma-separated list of IP address typesSLOT_NAME: The name of the replication slot to create.TABLE_LIST: A comma-separated list of tables to read change data from, in the format"schema_name"."table_name".TOPIC_PREFIX: A prefix to use for the Kafka topic names.
Configure the connector
This section describes some configuration properties that you can set on the connector. For a complete list, see Debezium connector for PostgreSQL in the Debezium documentation.
IP address types
The driver.ipTypes property specifies the type of IP address that the connector uses
to connect to the database:
PRIVATE: Private IPPSC: Private Service ConnectPUBLIC: Public IP
The driver.ipTypes property contains a comma-separated list of IP types in preferred
order; for example, driver.ipTypes=PRIVATE,PUBLIC.
For more information, see Configure networking.
Publication name
By default, the connector tries to stream from a publication named dbz_publication.
To specify a different publication, add
publication.name=PUBLICATION_NAME to the configuration, where
PUBLICATION_NAME is the publication name. Example:
publication.name=my_publication.
Replication slots
PostgreSQL uses replication slots to stream database table changes. By default,
the connector creates a replication slot named debezium. To use a different slot name,
set the slot.name property.
If you create two instances of the connector for the same database, you must specify a unique slot name for each connector.
By default, the connector sets the
slot.drop.on.stop property to false to prevent data loss. When you
permanently delete a connector, you must manually drop the replication slot that the connector
was using. The replication slot name defaults to debezium, unless configured
differently using the slot.name property.
We recommend that you set up alerts to monitor WAL disk usage on your source PostgreSQL database server, and drop any unused replication slots.
Table filter
By default, the connector captures change data from every non-system table in the database. To filter which tables are captured, specify one or more of the following settings:
schema.include.list. A list of schemas to include.schema.exclude.list. A list of schemas to exclude. Cannot be used withschema.include.list.table.include.list. A list of tables to include.table.exclude.list. A list of tables to exclude. Cannot be used withtable.include.list.
Topic names
By default, the connector creates Kafka topics with the following naming
convention: topic_prefix.schema.table_name, where topic.prefix is the
value of the topic.prefix configuration.
For more information, see Topic names in the Debezium documentation.
What's next
- Troubleshoot a PostgreSQL Source connector
- Troubleshoot Connect clusters and connectors
- Create a Generic PostgreSQL Source connector