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 Quan 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.
As a first step, please log in with your Lionmail or Barnard 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 or Barnard 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 or Barnard account.) After confirming that you are logged in with your Lionmail or Barnard 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:
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.
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”).
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.
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
<your_uni>
") when your teammate
activates your environment 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
When prompted that this would use up additional disk space, type y,
and press enter/return.
This command installs the following packages:
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:
pip
for Python 3:
sudo apt install python3-pip
virtualenvwrapper
package (this is the one time you
should install globally):sudo pip3 install virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
~/.bashrc
file, either using
an editor (like nano ~/.bashrc
) or running the following:echo "source /usr/local/bin/virtualenvwrapper.sh" >>
~/.bashrc
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
source .virtualenvs/dbproj/bin/activate
deactivate
Now let's install a set of useful packages into your environment:
pip3
(i.e., run pip3 install psycopg2 sqlalchemy click
and then run pip3 install flask
)
Let's make sure you have access to Python and PostgreSQL. First of all, activate your environment, as you did above.
Type python3
and ensure that you see the following
(the Python version may be a slightly different 3.X.X with a different
date as well):
Python 3.8.10 (default, Sep 11 2024, 16:02:53) [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()
.
We will use the PostgreSQL DBMS in this class. Check that the
client program works:
psql --version
If it prints something like the following, with a version 15.X, then it works:
psql (PostgreSQL) 15.8 (Ubuntu 15.8-1.pgdg20.04+1)
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.