Trick-or-Metric | GSP1084

Solution for Trick-or-Metric | GSP1084. 1 lab: GSP1084. Fast copy-paste commands for Google Cloud.

GSP1084 — Migrate to AlloyDB from PostgreSQL Using Database Migration Service

Estimated time: 40 minutes

# 🚀 Migrate to AlloyDB using Database Migration Service | GSP1084 > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. It is designed to help learners understand PostgreSQL migration, AlloyDB, and continuous database replication using Google Cloud Database Migration Service (DMS). Always attempt the lab yourself first and follow Google Cloud Skills Boost / Qwiklabs Terms of Servi

clear
CYAN=


\033[0;36m'
GREEN=


\033[0;32m'
YELLOW=


\033[0;33m'
BOLD=


\033[1m'
RESET=


\033[0m'
echo "${CYAN}${BOLD}"
echo "   ____        _   _ _            __   ___              "
echo "  / __ \      | | (_) |          / _| / _ \             "
echo " | |  | |_ __| |__  _| |_  ___   | |_ | | | |_ __ ___  "
echo " | |  | | '__| '_ \| | __| / _ \ |  _|| | | | '_ \ / __| "
echo " | |__| | |  | |_) | | |_ | (_) || |  | |_| | |_) \__ \ "
echo "  \____/|_|  |_.__/|_|\__| \___/ |_|   \___/| .__/|___/ "
echo "                                            | |         "
echo "                                            |_|         "
echo "${RESET}"
echo "${CYAN}${BOLD}==========================================================${RESET}"
echo "${CYAN}${BOLD} STAGE 1: Source Verification & Environment Setup         ${RESET}"
echo "${CYAN}${BOLD}==========================================================${RESET}"

echo "${YELLOW}▶ Configuring SSH keys...${RESET}"
mkdir -p ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/google_compute_engine -N "" -q <<< y >/dev/null 2>&1 || true

echo "${YELLOW}▶ Discovering Source Database IP...${RESET}"
export ZONE=$(gcloud compute instances list --filter="name:pg14-source" --format="value(zone)" | head -n 1)
export REGION=${ZONE%-*}
export SOURCE_IP=$(gcloud compute instances describe pg14-source --zone=$ZONE --format="value(networkInterfaces[0].networkIP)")

echo "${YELLOW}▶ Verifying Source Database Records (Task 1)...${RESET}"
gcloud compute ssh pg14-source --zone=$ZONE --quiet --command="sudo -u postgres psql -c '\dt'"
gcloud compute ssh pg14-source --zone=$ZONE --quiet --command="sudo -u postgres psql -c 'select count (*) as countries_row_count from countries;'"

echo "${CYAN}${BOLD}==========================================================${RESET}"
echo "${GREEN}${BOLD} MANUAL UI STEPS REQUIRED FOR TASK 2 (Database Migration) ${RESET}"
echo "${CYAN}${BOLD}==========================================================${RESET}"
echo "Leave this terminal open and follow these exact steps in the Cloud Console:"
echo ""
echo "1. Search for and open ${BOLD}Database Migration${RESET} in the top search bar."
echo "2. Go to ${BOLD}Migration jobs${RESET} (left pane) and click ${BOLD}Create migration job${RESET}."
echo "3. ${BOLD}Get started:${RESET}"
echo "   - Job name: ${BOLD}postgres-to-alloydb${RESET}"
echo "   - Source: ${BOLD}PostgreSQL${RESET}"
echo "   - Destination: ${BOLD}AlloyDB for PostgreSQL${RESET}"
echo "   - Click 'Save & continue'."
echo "4. ${BOLD}Define a source:${RESET}"
echo "   - Select ${BOLD}Create a connection profile${RESET}."
echo "   - Profile name: ${BOLD}pg14-source${RESET}"
echo "   - Internal IP: ${GREEN}${BOLD}$SOURCE_IP${RESET}  <-- (Use this exact IP)"
echo "   - Port: ${BOLD}5432${RESET}"
echo "   - Username: ${BOLD}postgres${RESET}"
echo "   - Password: ${BOLD}Change3Me${RESET}"
echo "   - Click 'Save', then 'Create', then 'Save & continue'."
echo "5. ${BOLD}Define destination:${RESET}"
echo "   - Destination type: ${BOLD}Existing cluster${RESET}"
echo "   - Cluster ID: ${BOLD}alloydb-target-cluster${RESET}"
echo "   - Click 'Select & continue' (Type the name again if prompted to confirm)."
echo "6. ${BOLD}Define connectivity method:${RESET}"
echo "   - Select ${BOLD}VPC peering${RESET}, click 'Configure & continue'."
echo "7. ${BOLD}Migration databases:${RESET} Select ${BOLD}All databases${RESET}, click 'Save & continue'."
echo "8. ${BOLD}Test and Create:${RESET} Click 'Test job'. Once successful, click ${BOLD}Create & start job${RESET}."
echo ""
echo "${YELLOW}${BOLD}⚠️ CRITICAL: Wait on the DMS page until the job status is 'Running' and phase is 'CDC'.${RESET}"
echo "${YELLOW}${BOLD}Once it is running, return here and run Command 2.${RESET}"
echo "${CYAN}${BOLD}==========================================================${RESET}"
CYAN=


\033[0;36m'
GREEN=


\033[0;32m'
YELLOW=


\033[0;33m'
BOLD=


\033[1m'
RESET=


\033[0m'

echo "${CYAN}${BOLD}==========================================================${RESET}"
echo "${CYAN}${BOLD} STAGE 2: Replication Validation & Live Update            ${RESET}"
echo "${CYAN}${BOLD}==========================================================${RESET}"

export ZONE=$(gcloud compute instances list --filter="name:pg14-source" --format="value(zone)" | head -n 1)
export REGION=${ZONE%-*}

echo "${YELLOW}▶ Injecting live data update ('Oceania') into Source Database (Task 4)...${RESET}"
gcloud compute ssh pg14-source --zone=$ZONE --quiet --command="sudo -u postgres psql -c \"insert into regions values (5, 'Oceania');\""

echo "${YELLOW}▶ Retrieving Target AlloyDB Private IP...${RESET}"
export ALLOYDB_IP=$(gcloud alloydb instances describe alloydb-target-instance --cluster=alloydb-target-cluster --region=$REGION --format="value(ipAddress)")

echo "${YELLOW}▶ Verifying continuous replication on AlloyDB Target (Task 3 & 4)...${RESET}"
gcloud compute ssh alloydb-client --zone=$ZONE --quiet --command="export PGPASSWORD=Change3Me; psql -h $ALLOYDB_IP -U postgres -c 'select count (*) as countries_row_count from countries; select region_id, region_name from regions;'"

echo "${GREEN}${BOLD}✓ Live update verified! The Oceania record successfully migrated to AlloyDB.${RESET}"
echo "${GREEN}${BOLD}You may now click 'Check my progress' for all remaining tasks.${RESET}"
echo "${CYAN}${BOLD}==========================================================${RESET}"