COMS W4111.001-Introduction to Databases
Fall 2023

Google Cloud Setup

Setting Up your Google Cloud Account

This page provides instructions on how to set up a Linux virtual machine and PostgreSQL database running on Google Cloud. We will be using Google's cloud computing infrastructure for Parts 2 and 3 of Project 1. You should have received an email from Lara with your individual Google Cloud code that will provide you with enough credit for the class. (This email also included the login credentials for your PostgreSQL database account.)

Caution: Running services (e.g., virtual machines) on any cloud provider costs money or credits. It can be very easy to spend more than you anticipated by leaving your services running. So please make it a habit to stop your services when not in use. If you run out of credits, there's not much that the class staff can do to help you out.

Signing Up and Setting Up the OS

As a first step, please log in with your Lionmail account in your browser and follow link console.cloud.google.com/education. After visiting this website, confirm that you are logged in with your Lionmail account by checking the top right of your screen. (If you enter your credit code into the wrong account, such as your personal Gmail account, you won't be able to transfer it later to your Lionmail account.) After confirming that you are logged in with your Lionmail account, enter the credit code ("coupon code") that we sent you by email.

Then, to set up your VM on the Google Cloud, follow these steps in a new browser tab:

  1. Go to console.cloud.google.com
  2. Click on the dropdown menu immediately to the right of "Google Cloud" on the top left of the page, and then on “NEW PROJECT” (if you have the option to select from multiple billing accounts, make sure you select the one with “Billing Account for Education” as your billing account name)
  3. Enter a “Project name” (e.g., “cs4111-001”) and click “CREATE”
  4. Go back to console.cloud.google.com and make sure that the project that you just created (e.g., “cs4111-001”) is selected on the top navigation bar, to the right of “Google Cloud”
  5. Click on the menu button in the top left hand corner and select the item “Compute Engine” and then “VM instances”; if prompted, click on "Enable" and/or "Set account"; please be patient: this step takes a few moments to complete
  6. Click “CREATE INSTANCE” to create your instance (roughly speaking, an instance is a computer that runs on the cloud)
  7. Type any name (for example, “cs4111-instance”)
  8. Change “Region” to “us-east1 (South Carolina)”
  9. Change “Zone” to any “us-east1-x” (the last letter doesn’t matter)
  10. Set "Machine type" to "n1-standard-1 (1 vCPU, 3.75 GB memory)"; if this option does not appear initially, click "Series" and select "N1" first
  11. Scroll down to "Boot disk," click "CHANGE" and then change "Operating system" to Ubuntu (if needed), and "Version" to "Ubuntu 20.04 LTS" with "x86/64" boot disk's architecture; change the boot disk size to 25 GB; finally click "SELECT"
  12. Check the "Allow HTTP traffic" and "Allow HTTPS traffic" boxes under "Firewall"
  13. Click "Create"; wait until the green checkmark shows up (Note: External IP is the address to access your instance. You will use it for Part 3 to access your web application if you follow the Web Front-End Option. This IP address changes every time that the instance is restarted, so don't bother writing it down.)

When a VM is created, it is automatically started as well. So please make sure that you stop the VM (see below) if you don't need to use it, to avoid wasting credits.

Using your VM

Background information: Each VM falls under a project on Google Cloud. To start working, you should always make sure that you are under the project that you created above for this class (e.g., “cs4111-001”).

  1. Go to console.cloud.google.com
  2. Select the correct project for the class (e.g., “cs4111-001”) at the top of the page, next to “Google Cloud”
  3. Click on the menu button in the top left hand corner and select the item “Compute Engine” and then “VM instances”
  4. Select the VM that you created above for the project (e.g., “cs4111-instance”) by checking the box next to its name and, if it's not running already, select “Start / Resume ” from the top menu (this process can take a minute)
  5. Once the VM has started up, there are two ways to connect:
    1. On the page where you start the VM, you can connect to the VM through the browser by clicking on “SSH” in the same row as the rest of the VM’s information. This opens a new browser window that you can interact with as you would with a Unix/Linux terminal.
    2. Alternatively, you can SSH into the VM by using the terminal, following the instructions available here.
  6. To turn off the VM when you are done using it, go to the “Compute Engine” page, select the VM, and select “Stop” from the top menu. Make sure you turn off the VM whenever you or your partner aren’t using it to avoid wasting credits.

Adding your Teammate to the VM

  1. Go to console.cloud.google.com
  2. Make sure that you are in the project that the VM is associated with (see above)
  3. Click on the menu button in the top left hand corner and select the item “IAM and Admin” (this item might be under "More Products")
  4. Click the “Grant Access” button near the top of the screen
  5. Type in your teammate’s Lionmail address in the box labeled “New Principals”
  6. In the dropdown menu labeled “Select a role,” select “Owner” to give your teammate full access to the resources of the project, then “Save”
    Note: if your teammate does not receive your email invitation for some reason, please ask them to use this URL template, with the right information filled in, to find and accept your invitation: https://console.cloud.google.com/invitation?project=[your project id]&account=[your email address]&memberEmail=[your teammate's email address] To find the Project ID, go to console.cloud.google.com and select your project instance via the dropdown menu. You should see the Project ID near the top of the page.
  7. Once your teammate accepts the invitation, your teammate will be able to use the VM as specified in the previous instructions; to switch to your virtual environment dbproj on your VM (see below for how to set up your virtual environment), your teammate should run:
    source ../<your_uni>/.virtualenvs/dbproj/bin/activate
    Note that your teammate should explicitly specify your UNI in the path above ("<your_uni>") when your teammate activates your environment in your VM.

Installing Additional Software in Your VM

You will need to install additional software in your VM for this project. Only one teammate needs to perform this step, as long as the other teammate is added to this VM following the instructions above. Before installing additional software, make sure to run:

sudo apt-get update

sudo apt-get -y upgrade

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql-pgdg.list > /dev/null

sudo apt-get update

Then, install some software packages that we will need, using the Ubuntu package management tool apt-get. Specifically, run the following command:

sudo apt-get install postgresql-15 postgresql-server-dev-15 git python3-virtualenv python3-dev

This command installs the following packages:

Setting Up Python

Python, which you will use for Part 3 of the project, uses its own package manager to install, update, and remove packages. In general, the following installs python packages:
pip3 install <packagename>

Typically the package manager will require sudo and install the packages in a global folder that affects everyone using your machine. This is bad practice because different Python applications may use different versions of packages and it's easy to step on each other's toes.

We will use virtualenv to create virtual environments that contain their own copies of python and packages. When we work in a virtual environment, pip will install packages local to the environment rather than globally. If you are interested, you can read a detailed tutorial. We already installed the virtualenv command with apt-get above.

So let's set up your environment:

  1. First install pip for Python 3:
    sudo apt install python3-pip
  2. Install some helper commands from the virtualenvwrapper package (this is the one time you should install globally):
    sudo pip3 install virtualenvwrapper
  3. Load the wrapper commands in the current shell:
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
    source /usr/local/bin/virtualenvwrapper.sh
  4. To make sure this takes effect each time you log in, add that command to the end of your ~/.bashrc file, either using an editor (like nano ~/.bashrc) or running the following:
    echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
  5. Create a new environment dbproj (you can of course choose a different name, which you should then substitute for dbproj in the rest of the instructions); this will create a folder dbproj/ in ~/.virtualenvs/):
    mkvirtualenv dbproj
  6. Switch to, or activate, the environment by running:
    source .virtualenvs/dbproj/bin/activate
  7. Switch out of the environment:
    deactivate

Now let's install a set of useful packages into your environment:

  1. Activate your environment, as you did above
  2. Install the following packages using pip3 (i.e., run pip3 install psycopg2 sqlalchemy click and then run pip3 install flask)
    • psycopg2
    • sqlalchemy
    • click
    • flask
  3. Deactivate (and you're done!)

Testing that Things Work

Let's make sure you have access to Python and PostgreSQL. First of all, activate your environment, as you did above.

Checking Python

Type python3 and ensure that you see the following (the Python version may be a slightly different 3.8.X):

    Python 3.8.10 (default, Jun 22 2022, 20:18:18)
    [GCC 9.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

Then try importing some modules from the packages we installed:
>>> import flask
>>> import psycopg2
>>> import sqlalchemy
>>> import click

If that worked, push ctrl+d to exit the prompt or, alternatively, type quit().

Checking PostgreSQL

We will use the PostgreSQL DBMS in this class. Check that the client program works:
psql --version

If it prints something like the following then it works:

psql (PostgreSQL) 15.4 (Ubuntu 15.4-2.pgdg20.04+1)

Stopping Your Virtual Machine

While you have abundant credit for this class on Google Cloud, it is very easy to accidentally use up all of your credits. To conserve your credits --and also to avoid wasting energy-- make sure to turn off your machine whenever you are not using it, following the instructions under "Using your VM" above.