Create a database for replication

Controlla i miei progressi

/ 50

Create the Datastream resources

Controlla i miei progressi

/ 50

Questo lab potrebbe incorporare strumenti di AI a supporto del tuo apprendimento.
Questi contenuti non sono ancora ottimizzati per i dispositivi mobili.
Per un'esperienza ottimale, visualizza il sito su un computer utilizzando un link inviato via email.

GSP1052

Google Cloud self-paced labs

Overview

In today's competitive environment, organizations need to quickly and easily make decisions based on real-time data. Datastream for BigQuery features seamless replication from operational database sources such as AlloyDB, MySQL, PostgreSQL, and Oracle, directly into BigQuery, Google Cloud's serverless data warehouse. With a serverless, autoscaling architecture, Datastream lets you easily set up an ELT (Extract, Load, Transform) pipeline for low-latency data replication that enables real-time insights.

In this hands-on lab, you deploy a Cloud SQL for PostgreSQL database and import a sample dataset using the gcloud CLI. In the Google Cloud console, you create and start a Datastream stream and replicate data into BigQuery.

Although you can easily copy and paste commands from the lab to the appropriate place, type the commands yourself to reinforce your understanding of the core concepts.

What you'll do

In this lab, you learn how to perform the following tasks:

  • Prepare a Cloud SQL for PostgreSQL instance using the Google Cloud console.
  • Import data into the Cloud SQL instance.
  • Create a Datastream connection profile for the PostgreSQL database.
  • Create a Datastream connection profile for the BigQuery destination.
  • Create a Datastream stream and start replication.
  • Validate that Datastream replicates existing data and changes into BigQuery.

Prerequisites

  • Familiarity with standard Linux environments
  • Familiarity with change data capture (CDC) concepts

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.

This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the right is the Lab setup and access panel with the following:

    • The Open Google Cloud console button
    • The temporary credentials (username and password) that you must use for this lab
    • Other information, if needed, to step through this lab

    Note that the lab timer is located near the top of the page, showing the remaining time.

  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab setup and access panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab setup and access panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field. Navigation menu icon and Search field

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

  2. Click through the following windows:

    • Continue through the Cloud Shell information window.
    • Authorize Cloud Shell to use your credentials to make Google Cloud API calls.

When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Create a database for replication

In this task, you prepare a Cloud SQL for PostgreSQL database for Datastream replication.

Create the Cloud SQL database

  1. Run the following command to enable the Cloud SQL API:
gcloud services enable sqladmin.googleapis.com
  1. Run the following command to create a Cloud SQL for PostgreSQL database instance:
POSTGRES_INSTANCE=postgres-db DATASTREAM_IPS={{{project_0.startup_script.ip_Address | IP_ADDRESS}}} gcloud sql instances create ${POSTGRES_INSTANCE} \ --database-version={{{project_0.startup_script.postgres_id_long | POSTGRES_14}}} \ --cpu=2 --memory=10GB \ --authorized-networks=${DATASTREAM_IPS} \ --region={{{project_0.default_region|REGION}}} \ --root-password pwd \ --database-flags=cloudsql.logical_decoding=on Note: This command creates the database in . For other regions, replace DATASTREAM_IPS with the Datastream public IPs for your region. Note: After you create the database instance, note the instance's public IP address because you need it later when you create the Datastream connection profile.

Click Check my progress to verify the objective.

Create a database for replication

Populate the database with sample data

  1. In Cloud Shell, run the following command to connect to the PostgreSQL database:
gcloud sql connect postgres-db --user=postgres
  1. When prompted for the password, enter pwd and press ENTER.

  2. After you connect to the database, run the following SQL command to create a sample schema and table:

CREATE SCHEMA IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.example_table ( id SERIAL PRIMARY KEY, text_col VARCHAR(50), int_col INT, date_col TIMESTAMP ); ALTER TABLE test.example_table REPLICA IDENTITY DEFAULT; INSERT INTO test.example_table (text_col, int_col, date_col) VALUES ('hello', 0, '2020-01-01 00:00:00'), ('goodbye', 1, NULL), ('name', -987, NOW()), ('other', 2786, '2021-01-01 00:00:00');

Configure the database for replication

  • Run the following SQL command to create a publication and a replication slot:
CREATE PUBLICATION test_publication FOR ALL TABLES; ALTER USER POSTGRES WITH REPLICATION; SELECT PG_CREATE_LOGICAL_REPLICATION_SLOT('test_replication', 'pgoutput');

Task 2. Create the Datastream resources and start replication

Now that the database is ready, create the Datastream connection profiles and stream to begin replication.

  1. In the Google Cloud console, on the Navigation menu, click View all products > Analytics > Datastream.

  2. Click Enable to enable the Datastream API.

Datastream API enablement page with the Enable button

Create the PostgreSQL connection profile

Create two connection profiles: one for the PostgreSQL source, and another for the BigQuery destination.

  1. In the Google Cloud console, on the Connection profiles tab, click Create profile.

  2. Select the PostgreSQL connection profile type.

PostgreSQL connection profile type option
  1. For Connection profile name, type postgres-cp

  2. Enter the database connection details:

    • For Region, select .
    • For Hostname or IP and Port, enter the public IP address and port of the Cloud SQL instance that you created in Task 1. To find the IP address of your Cloud SQL instance:
      • On the Navigation menu, click Cloud SQL.
      • On the Cloud SQL Instances page, locate your PostgreSQL instance named postgres-db.
      • Copy the public IP address of the instance.
    • For Username, type postgres
    • For Password, select Enter password manually and type pwd
    • For Database, type postgres
  3. Click Continue.

  4. For Encryption type, select None.

  5. Select the IP allowlisting connectivity method, and then click Continue.

  6. Click Run test to verify that Datastream can reach the database.

  7. Click Create.

Create the BigQuery connection profile

  1. In the Google Cloud console, on the Connection profiles tab, click Create profile.
Connection profiles page showing the Create profile button
  1. Select the BigQuery connection profile type.
BigQuery connection profile type option
  1. For Connection profile name, type bigquery-cp

  2. For Region, select .

  3. Click Create.

Create a stream

Create the stream that connects the connection profiles you created and defines the configuration for streaming data from the source to the destination.

  • In the Google Cloud console, on the Streams tab, click Create stream.
Streams page showing the Create stream button

Define the stream details

  1. For Stream name, type test-stream
  2. For Region, select .
  3. For Source type, select PostgreSQL.
  4. For Destination type, select BigQuery.
  5. Click Continue.

Define and test the source

  1. For Connection profile name, select the postgres-cp connection profile that you created earlier.
  2. Optional: Click Run test to test connectivity.
  3. Click Continue.

Configure the source

  1. For Replication slot name, type test_replication
  2. For Publication name, type test_publication
  3. Select the test schema for replication.
  4. Click Continue.

Define the destination

  • For Connection profile name, select the bigquery-cp connection profile that you created earlier, and then click Continue.

Configure the destination

  1. For BigQuery dataset location, select Region, and then select .
  2. For Staleness limit, select 0 seconds.
  3. Click Continue.

Review and create the stream

  1. Click Run validation to validate the stream details.
  2. After validation finishes, click Create & start, and then click Create & start in the confirmation dialog.

Wait approximately 1-2 minutes until the stream status shows Running.

Click Check my progress to verify the objective.

Create the Datastream resources

Task 3. View the data in BigQuery

Now that the stream is running, view the data replicating into the BigQuery dataset.

  1. In the Google Cloud console, on the Navigation menu, click BigQuery. If a welcome dialog appears, click Done.

  2. In the Explorer pane, expand your project node to view the list of datasets.

  3. Expand the test dataset node.

  4. Click the example_table table.

  5. Click the Preview tab to view the data in BigQuery.

Note: The data might take a few minutes to appear on the Preview tab. BigQuery Explorer page with the example_table visible
  1. If the message "There is no data to display" appears on the Preview tab, open a query editor tab and run the following SQL query to verify the data:
SELECT * FROM test.example_table ORDER BY id;

Task 4. Check that Datastream replicates source changes to BigQuery

  1. In Cloud Shell, run the following command to connect to the Cloud SQL database (when prompted for the password, enter pwd):
gcloud sql connect postgres-db --user=postgres
  1. Run the following SQL commands to make some changes to the data:
INSERT INTO test.example_table (text_col, int_col, date_col) VALUES ('abc', 0, '2022-10-01 00:00:00'), ('def', 1, NULL), ('ghi', -987, NOW()); UPDATE test.example_table SET int_col=int_col*2; DELETE FROM test.example_table WHERE text_col = 'abc';
  1. In BigQuery Studio, run the following query to view the replicated changes in BigQuery:
SELECT * FROM test.example_table ORDER BY id; BigQuery query results showing the updated rows in example_table

Congratulations!

Datastream is an important tool in your data integration and analytics toolkit! You have learned the basics of replication from PostgreSQL to BigQuery with Datastream.

Manual Last Updated November 12, 2026

Lab Last Tested August 3, 2026

Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Prima di iniziare

  1. I lab creano un progetto e risorse Google Cloud per un periodo di tempo prestabilito
  2. I lab hanno un limite di tempo e non possono essere messi in pausa. Se termini il lab, dovrai ricominciare dall'inizio.
  3. In alto a sinistra dello schermo, fai clic su Inizia il lab per iniziare

Utilizza la navigazione privata

  1. Copia il nome utente e la password forniti per il lab
  2. Fai clic su Apri console in modalità privata

Accedi alla console

  1. Accedi utilizzando le tue credenziali del lab. L'utilizzo di altre credenziali potrebbe causare errori oppure l'addebito di costi.
  2. Accetta i termini e salta la pagina di ripristino delle risorse
  3. Non fare clic su Termina lab a meno che tu non abbia terminato il lab o non voglia riavviarlo, perché il tuo lavoro verrà eliminato e il progetto verrà rimosso

Questi contenuti non sono al momento disponibili

Possiamo inviarti una notifica via email quando sarà disponibile

Bene.

Ti contatteremo via email non appena sarà disponibile

Un lab alla volta

Conferma per terminare tutti i lab esistenti e iniziare questo

Utilizza la navigazione privata per eseguire il lab

Il modo migliore per eseguire questo lab è utilizzare una finestra del browser in incognito o privata. Ciò evita eventuali conflitti tra il tuo account personale e l'account studente, che potrebbero causare addebiti aggiuntivi sul tuo account personale.