CREATE USER USER_NAME WITH REPLICATION LOGIN PASSWORD 'USER_PASSWORD';
更改下列內容:
USER_NAME:您要建立的 Datastream 使用者名稱。
USER_PASSWORD:您要建立的 Datastream 使用者登入密碼。
將下列權限授予您建立的使用者:
GRANT SELECT ON ALL TABLES IN SCHEMA SCHEMA_NAME TO USER_NAME;
GRANT USAGE ON SCHEMA SCHEMA_NAME TO USER_NAME;
ALTER DEFAULT PRIVILEGES IN SCHEMA SCHEMA_NAME GRANT SELECT ON TABLES TO USER_NAME;
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-12 (世界標準時間)。"],[[["\u003cp\u003eThis guide explains how to configure an AlloyDB for PostgreSQL database to work with Datastream, beginning with setting up a TCP proxy for connectivity.\u003c/p\u003e\n"],["\u003cp\u003eTo enable Datastream's connection to the AlloyDB instance, a TCP proxy must be launched in the consumer project using a provided script, replacing placeholders with appropriate values.\u003c/p\u003e\n"],["\u003cp\u003eLogical decoding must be enabled on the AlloyDB primary instance, along with granting replication privileges to a designated database user via the provided command, and creating a publication for tables to be replicated.\u003c/p\u003e\n"],["\u003cp\u003eA dedicated Datastream user must be created with specific privileges, including replication login and select permissions on tables and schemas, using the provided commands.\u003c/p\u003e\n"],["\u003cp\u003eAfter completing the setup, you can use the TCP proxy IP address for the connection profile, and create a stream in the Datastream stream creation wizard using the publication and replication slot names.\u003c/p\u003e\n"]]],[],null,["# Configure an AlloyDB for PostgreSQL database\n\nThe following sections cover how to configure an AlloyDB for PostgreSQL database.\n\nSet up connectivity between Datastream and AlloyDB\n--------------------------------------------------\n\nTo enable Datastream to connect to the AlloyDB instance, you need to set up a TCP proxy in the consumer project. The [TCP proxy image](http://gcr.io/dms-images/tcp-proxy) is publicly available to all customers.\n\n1. Launch the proxy container:\n\n 1. Activate Cloud Shell in the consumer project.\n 2. Run the following script to create the TCP proxy and disable bridge networking to avoid connectivity issues:\n\n gcloud compute instances create-with-container \\\n --zone=\u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e \\\n --container-image gcr.io/dms-images/tcp-proxy \\\n --tags=dms-tcp-proxy \\\n --container-env=SOURCE_CONFIG=\u003cvar translate=\"no\"\u003eALLOYDB_IP\u003c/var\u003e:\u003cvar translate=\"no\"\u003eALLOYDB_PORT\u003c/var\u003e \\\n --can-ip-forward \\\n --network=\u003cvar translate=\"no\"\u003eSOURCE_AND_DEST_VPC\u003c/var\u003e \\\n --machine-type=\u003cvar translate=\"no\"\u003eVM_TIER\u003c/var\u003e \\\n --metadata=startup-script='#! /bin/bash\n mkdir -p /etc/docker\n cat \u003c\u003cEOF \u003e /etc/docker/daemon.json\n {\"bridge\":\"none\"}\n EOF\n systemctl restart docker'\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e: The region in which you want to create the TCP proxy.\n - \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e: The name of the virtual machine.\n - \u003cvar translate=\"no\"\u003eALLOYDB_IP\u003c/var\u003e: The IP address of the AlloyDB for PostgreSQL instance.\n - \u003cvar translate=\"no\"\u003eALLOYDB_PORT\u003c/var\u003e: The port number of the AlloyDB for PostgreSQL instance.\n - \u003cvar translate=\"no\"\u003eSOURCE_AND_DEST_VPC\u003c/var\u003e: The VPC network to which the source and destination are connected.\n - \u003cvar translate=\"no\"\u003eVM_TIER\u003c/var\u003e: The type of your virtual machine.\n\n An example command with updated parameters: \n\n gcloud compute instances create-with-container \\\n --zone=us-central1-c ds-tcp-proxy \\\n --container-image gcr.io/dms-images/tcp-proxy \\\n --tags=ds-tcp-proxy \\\n --container-env=SOURCE_CONFIG=10.16.0.5:5432 \\\n --can-ip-forward \\\n --network=default \\\n --machine-type=e2-micro\n --metadata=startup-script='#! /bin/bash\n mkdir -p /etc/docker\n cat \u003c\u003cEOF \u003e /etc/docker/daemon.json\n {\"bridge\":\"none\"}\n EOF\n systemctl restart docker'\n\n | You may receive a prompt to authorize Cloud Shell. Click **AUTHORIZE**.\n2. Run the script to print the IP address of the TCP Proxy with the following inputs:\n\n gcloud compute instances describe \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e \\\n --format=\"yaml(networkInterfaces[].networkIP)\" \\\n --zone=\u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e: The name of your virtual machine.\n - \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e: The region in which you created the TCP proxy.\n\n | Use this IP address in the Datastream [connection profile](/datastream/docs/create-connection-profiles#cp4postgresdb) configuration.\n3. (Optional) Create a firewall rule to limit ingress traffic to the TCP proxy:\n\n gcloud compute firewall-rules create \u003cvar translate=\"no\"\u003eFIREWALL_RULE_NAME\u003c/var\u003e \\\n --direction=INGRESS \\\n --priority=1000 \\\n --target-tags=dms-tcp-proxy \\\n --network=\u003cvar translate=\"no\"\u003eSOURCE_VPC\u003c/var\u003e \\\n --action=ALLOW \\\n --rules=tcp:\u003cvar translate=\"no\"\u003eALLOYDB_PORT\u003c/var\u003e \\\n --source-ranges=\u003cvar translate=\"no\"\u003eIP_RANGE\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eFIREWALL_RULE_NAME\u003c/var\u003e: The name of your firewall rule.\n - \u003cvar translate=\"no\"\u003eSOURCE_VPC\u003c/var\u003e: The VPC network to which your source is connected.\n - \u003cvar translate=\"no\"\u003eALLOYDB_PORT\u003c/var\u003e: The port number of the AlloyDB for PostgreSQL instance.\n - \u003cvar translate=\"no\"\u003eIP_RANGE\u003c/var\u003e: The IP address range to which you want to limit ingress traffic.\n\n An example command to allow ingress traffic from all IP addresses: \n\n gcloud compute firewall-rules create ds-proxy1 \\\n --direction=INGRESS \\\n --priority=1000 \\\n --target-tags=ds-tcp-proxy \\\n --network=default \\\n --action=ALLOW \\\n --rules=tcp:5432\n\nConfigure AlloyDB for replication\n---------------------------------\n\n1. Enable logical decoding for the AlloyDB primary instance. In Google Cloud, set\n the value for the `alloydb.logical_decoding` flag to **ON** . For information about\n updating the database flags, see [Configure an instance's database flags](/alloydb/docs/instance-configure-database-flags).\n\n | **Note:** Updating the `alloydb.logical_decoding` flag restarts the instance.\n2. Connect to the AlloyDB instance using the TCP proxy IP address by running the\n following command from any VM on the same VPC:\n\n psql -h \u003cvar translate=\"no\"\u003ePROXY_IP\u003c/var\u003e \\\n -U \u003cvar translate=\"no\"\u003eDB_USER\u003c/var\u003e \\\n -d \u003cvar translate=\"no\"\u003eDB_NAME\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROXY_IP\u003c/var\u003e: The IP address of the TCP proxy.\n - \u003cvar translate=\"no\"\u003eDB_USER\u003c/var\u003e: The username for the AlloyDB database.\n - \u003cvar translate=\"no\"\u003eDB_NAME\u003c/var\u003e: The AlloyDB database name.\n3. Run the following command to grant replication privileges to your database user:\n\n ```\n ALTER USER DB_USER WITH REPLICATION;\n ```\n4. Create a publication. We recommend that you create a publication only for the\n tables that you want to replicate. This allows Datastream to read-only\n the relevant data, and lowers the load on the database and Datastream:\n\n ```\n CREATE PUBLICATION PUBLICATION_NAME\n FOR TABLE SCHEMA1.TABLE1, SCHEMA2.TABLE2;\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePUBLICATION_NAME\u003c/var\u003e: The name of your publication. You'll need to provide this name when you create a stream in the Datastream stream creation wizard.\n - \u003cvar translate=\"no\"\u003eSCHEMA\u003c/var\u003e: The name of the schema that contains the table.\n - \u003cvar translate=\"no\"\u003eTABLE\u003c/var\u003e: The name of the table that you want to replicate.\n\n You can also create a publication for all tables in your database. Note that\n this approach increases the load on both the source database and Datastream: \n\n ```\n CREATE PUBLICATION PUBLICATION_NAME FOR ALL TABLES;\n ```\n | For PostgreSQL 15 and later, you can create a publication for all tables in a schema. This approach lets you replicate changes for tables in the specified list of schemas, including tables that you create in the future: \n |\n | ```\n | CREATE PUBLICATION PUBLICATION_NAME\n | FOR TABLES IN SCHEMA SCHEMA1, SCHEMA2;\n | ```\n5. Create a replication slot by executing the following command.\n\n ```\n SELECT PG_CREATE_LOGICAL_REPLICATION_SLOT('REPLICATION_SLOT_NAME', 'pgoutput');\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eREPLICATION_SLOT_NAME\u003c/var\u003e: The name of your replication slot. You'll need to provide this name when you create a stream in the Datastream stream creation wizard.\n\nCreate a Datastream user\n------------------------\n\n1. To create a Datastream user, enter the following PostgreSQL command:\n\n ```\n CREATE USER USER_NAME WITH REPLICATION LOGIN PASSWORD 'USER_PASSWORD';\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e: The name of the Datastream user that you want to create.\n - \u003cvar translate=\"no\"\u003eUSER_PASSWORD\u003c/var\u003e: The login password for the Datastream user that you want to create.\n2. Grant the following privileges to the user you created:\n\n ```\n GRANT SELECT ON ALL TABLES IN SCHEMA SCHEMA_NAME TO USER_NAME;\n GRANT USAGE ON SCHEMA SCHEMA_NAME TO USER_NAME;\n ALTER DEFAULT PRIVILEGES IN SCHEMA SCHEMA_NAME GRANT SELECT ON TABLES TO USER_NAME;\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eSCHEMA_NAME\u003c/var\u003e: The name of the schema to which you want to grant the privileges.\n - \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e: The user to whom you want to grant the privileges.\n\n | The first command might display warnings, however it is safe to ignore them.\n\nWhat's next\n-----------\n\n- Learn more about how Datastream works with [PostgreSQL sources](/datastream/docs/sources-postgresql)."]]