Computing: Setting Up PostgreSQL on Cloud SQL

● Create a G oogle Cloud account if you do not already have one. See email from Professor MacLeod with link. ● Read through the G oogle Cloud Platform Overview to understand how Cloud works. ● Follow these i nstructions to install the Google Cloud SDK. ● Go to the G oogle Cloud Console. When first logged in (and new to Google Cloud), you get the following page:

Select C reate t o create a new project. You will be directed to the following page and asked to provide a project name. Enter p atient-app and select C reate (I f you already have a project active, click on the name in the menu bar & you will see the option to create a new project).

● Once created you will be brought to the dashboard page for your project. On this page you will find a summary of your project. Some of this includes your billing information, project information, the resources used, and error reporting.

● To run PostgreSQL on the cloud we need to create a using Cloud SQL. Select SQL from the Navigation menu (upper left three horiz. bars). You will be brought to this page. Select Create Instance.

● Under Choose Database Instance select PostgreSQL Version 10 (or 11). Then click Next. It may take a few minutes to setup the Compute Engine API to create the Database. ● Once ready you will be brought to the Create Instance Page. Fill in the Instance Id, and set default user password so you can log into the database. Then select Create. Again, it will take few minutes to setup the database.

● Once created you will be brought to a list of your . Select your database. You will be brought to a page that looks like this:

Here is where all of the information about your database is found. You can add users via the Users tab and add Databases via the Databases tab.

● We will connect to our database using the Cloud Shell. Under Connect to this instance, select Connect using Cloud Shell.

A cloud shell terminal will open at the bottom of your screen. It should have something similar to the following command setup for you: g cloud connect --user=postgres --quiet. H it enter to run it. You will get the following message : Whitelisting your IP for incoming connection for 5 minutes...⠶ followed by a prompt to enter your password for the postgres user. After entering it you will be brought to PSQL.

● Exercise: With PSQL running we can now create a table and add real data: ○ Create a New Database called g hs using the statement C REATE DATABASE ghs; and switch to the database using the following command: \ connect ghs or \c ghs. Create a new table called patients with an Id, First Name, Last Name, Gender, Address, and Birth Date column. (C reating a New Table) ■ CREATE TABLE IF NOT EXISTS patients ( id SERIAL PRIMARY KEY, firstName VARCHAR(255), lastName VARCHAR(255), gender VARCHAR(255), address VARCHAR(255), birthDate DATE); ○ Insert into a couple of new patients using the INSERT command (P opulating a Table) ○ Query the patients table to display all patients (Q uerying a Table) ○ Postgres/SQL cheat sheet for reference

● When done working with the database you can exit PSQL using \q. To exit the root shell enter exit. Enter exit again to close the SSH connection.

● When the SSH connection is closed stop your running virtual machine instance by selecting your virtual machine and clicking S top.

● GUI access to Postgres : T he above process should be sufficient to complete the project, but the command line access to postgres can get tedious for development. If you would like to configure remote access to the DB using a GUI tool (like pgadmin): ○ You can specify your computer’s IP using the connection tab on the left hand side (google search what is my ip to get your IP) ○ In Cloud SQL, select Connections under the Master Instance menu for your Cloud SQL database. Under connectivity scroll down to Public IP. If not checked off, check it. Then hit Add Network. Give the network a name, and then enter your IP address for your computer. Hit save.

■ If this step is not done, then you may get an error in PgAdmin stating that it cannot connect to the server in the next step. ○ To then connect to pgadmin: ■ Start PgAdmin. Under Dashboard select Add New Server. ■ In General, enter a server name. Under Connection enter the public IP listed on the GCP Postgres instance page. Put in the port # (default : 5432) Enter the password for the postgres user. Hit save. Your cloud database will appear in the list of servers.

When done remember to shutdown the database instance by opening your database instance and clicking stop located in the Instance details bar.