-
Make sure you have a Google Cloud project and billing is enabled.
-
Set your
PROJECT_ID
environment variable:export PROJECT_ID=<YOUR_PROJECT_ID>
-
Install the gcloud CLI.
-
Set gcloud project:
gcloud config set project $PROJECT_ID
-
Enable APIs:
gcloud services enable alloydb.googleapis.com \ compute.googleapis.com \ cloudresourcemanager.googleapis.com \ servicenetworking.googleapis.com \ vpcaccess.googleapis.com \ aiplatform.googleapis.com
-
Install python and set up a python virtual environment.
-
Make sure you have python version 3.11+ installed.
python -V
-
Download and install postgres-client cli (
psql
).
In this step, we will enable Private Services Access so that AlloyDB is able to connect to your VPC. You should only need to do this once per VPC (per project).
-
Set environment variables:
export RANGE_NAME=my-allocated-range-default export DESCRIPTION="peering range for alloydb-service"
-
Create an allocated IP address range:
gcloud compute addresses create $RANGE_NAME \ --global \ --purpose=VPC_PEERING \ --prefix-length=16 \ --description="$DESCRIPTION" \ --network=default
-
Create a private connection:
gcloud services vpc-peerings connect \ --service=servicenetworking.googleapis.com \ --ranges="$RANGE_NAME" \ --network=default
-
Set environment variables. For security reasons, use a different password for
$DB_PASS
and note it for future use:export CLUSTER=my-alloydb-cluster export DB_PASS=my-alloydb-pass export INSTANCE=my-alloydb-instance export REGION=us-central1
-
Create an AlloyDB cluster:
gcloud alloydb clusters create $CLUSTER \ --password=$DB_PASS\ --network=default \ --region=$REGION \ --project=$PROJECT_ID
-
Create a primary instance:
gcloud alloydb instances create $INSTANCE \ --instance-type=PRIMARY \ --cpu-count=8 \ --region=$REGION \ --cluster=$CLUSTER \ --project=$PROJECT_ID \ --ssl-mode=ALLOW_UNENCRYPTED_AND_ENCRYPTED
-
Get AlloyDB IP address:
export ALLOYDB_IP=$(gcloud alloydb instances describe $INSTANCE \ --cluster=$CLUSTER \ --region=$REGION \ --format 'value(ipAddress)')
-
Note the AlloyDB IP address for later use:
echo $ALLOYDB_IP
AlloyDB supports network connectivity through private, internal IP addresses only. For this section, we will create a Google Cloud Engine VM in the same VPC as the AlloyDB cluster. We can use this VM to connect to our AlloyDB cluster using Private IP.
-
Set environment variables:
export ZONE=us-central1-a export PROJECT_NUM=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)") export VM_INSTANCE=alloydb-proxy-vm
-
Create a Compute Engine VM:
gcloud compute instances create $VM_INSTANCE \ --project=$PROJECT_ID \ --zone=$ZONE \ --machine-type=e2-medium \ --network-interface=network-tier=PREMIUM,stack-type=IPV4_ONLY,subnet=default \ --maintenance-policy=MIGRATE \ --provisioning-model=STANDARD \ --service-account=$PROJECT_NUM[email protected] \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --create-disk=auto-delete=yes,boot=yes,device-name=$VM_INSTANCE,image-family=ubuntu-2004-lts,image-project=ubuntu-os-cloud,mode=rw,size=10,type=projects/$PROJECT_ID/zones/$ZONE/diskTypes/pd-balanced \ --no-shielded-secure-boot \ --shielded-vtpm \ --shielded-integrity-monitoring \ --labels=goog-ec-src=vm_add-gcloud \ --reservation-affinity=any
-
Create an SSH tunnel through your GCE VM using port forwarding. This will listen to
127.0.0.1:5432
and forward through the GCE VM to your AlloyDB instance:gcloud compute ssh --project=$PROJECT_ID --zone=$ZONE $VM_INSTANCE \ -- -NL 5432:$ALLOYDB_IP:5432
You will need to allow this command to run while you are connecting to AlloyDB. You may wish to open a new terminal to connect with.
-
Verify you can connect to your instance with the
psql
tool. Enter password for AlloyDB ($DB_PASS
environment variable set above) when prompted:psql -h 127.0.0.1 -U postgres
-
While connected using
psql
, create a database and switch to it:CREATE DATABASE assistantdemo; \c assistantdemo
-
Install
pgvector
extension in the database:CREATE EXTENSION vector;
-
Exit from
psql
:exit
-
Change into the retrieval service directory:
cd genai-databases-retrieval-app/retrieval_service
-
Install requirements:
pip install -r requirements.txt
-
Make a copy of
example-config.yml
and name itconfig.yml
.cp example-config.yml config.yml
-
Update
config.yml
with your database information. Keep using127.0.0.1
as the datastore host IP address for port forwarding.host: 0.0.0.0 datastore: # Example for postgres.py provider kind: "postgres" host: 127.0.0.1 port: 5432 # Update this with the database name database: "assistantdemo" # Update with database user, the default is `postgres` user: "postgres" # Update with database user password password: "my-alloydb-pass"
-
Populate data into database:
python run_database_init.py
Clean up after completing the demo.
-
Set environment variables:
export VM_INSTANCE=alloydb-proxy-vm export CLUSTER=my-alloydb-cluster export REGION=us-central1 export RANGE_NAME=my-allocated-range-default
-
Delete Compute Engine VM:
gcloud compute instances delete $VM_INSTANCE
-
Delete AlloyDB cluster that contains instances:
gcloud alloydb clusters delete $CLUSTER \ --force \ --region=$REGION \ --project=$PROJECT_ID
-
Delete an allocated IP address range:
gcloud compute addresses delete $RANGE_NAME \ --global