Bonus Features

Now that you have successfully deployed your app I will give you a few tips on how to interact with it. The first tip is how to set up your Conda environment for seamless local development. The second will describe how to connect and interact with your database remotely. Both tips rely on the configuration of the settings.py file described earlier.

Customizing Conda for Local Development

With this configuration your environment variables will automatically be loaded and removed when activating or deactivating the environment using Conda.

The trick here is to write a shell script that will load and export the environment variables for use in your app. The second script is used to remove the environment variables when the environment is deactivated. Both of them rely on hooking into the native Conda environment setup and tear-down processes.

First navigate to the directory of the Conda environment for your project. If you followed the default installation for Conda then the directory with all of your environments will be under /anaconda3/envs/ENV_NAME . Just add the name of your environment to the end of the path.

Once you are in the environment directory you need to create some new directories that Conda will check when activating / deactivating your environment. Enter the following commands to create the directories:

mkdir -p etc/conda/activate.d etc/conda/deactivate.d

The -p flag is used to make parent directories. If you don’t include this flag then you will get an error when trying to make nested directories like the command above.

Within the activate.d directory we will add the first env_vars.sh file. Navigate to it and enter vim env_vars.sh to open VIM and paste in the following lines. Edit the lines by hitting I on your keyboard (to insert) and esc when done typing. Finally enter :x and ENTER to save and close the editor.

Alternatively you can clone this gist into the directory and customize it in another text editor.

Next we write a similar script for hooking into the Conda deactivation process. This one goes in etc/conda/deactivate.d

Now every time you activate your Django project environment you will also execute the script to load the environment variables for local development. Accordingly those variables will be removed when the environment is deactivated.

Installing the Google Cloud SQL Proxy Tool

If you want to connect to your Google Cloud PostgreSQL instance remotely you will need a connection mechanism. One easy and secure way to connect is to install and configure the Google Cloud SQL Proxy Tool.

If you don’t plan on connecting to your database remotely you can skip this section. The remainder of these instructions will be using this tool though so if you are unsure I’d suggest following this section.

If you want to read the full instructions, installation for alternate operating systems / architectures, and details about how it works (pretty neat) here is the documentation.

For 64 bit OSX machines you can install by entering the following commands into your Terminal

Make sure you are in your home directory [ ~ ]

then enter the following to make the file executable

chmod +x cloud_sql_proxy

Using the Cloud SQL Proxy Tool

You may have noticed that the environment activation script from earlier had a host and port of 127.0.0.1 and 3306 respectively. These are the connection parameters we will use to connect to our database instance through the proxy server tool. To use the proxy tool you first need to get the connection name for your database instance:

gcloud beta sql instances describe DATABASE NAME

The second line of the output contains the Connect Name field you need

Next create a proxy connection by entering the data from the connectionName property between the quotes below. Note: if you change the port you will need to reflect that change in the environment activation script.

~/cloud_sql_proxy -instances="CONNECTION-NAME"=tcp:3306

Depending on how you authenticated with the Google Cloud SDK you may receive the following error:

google: could not find default credentials [Cloud SQL Proxy tool]

If this happens just run the following command to set the default credentials:

gcloud auth application-default login

This will open your default browser and request permission to set the default credentials. Try the sql proxy tool again after authenticating to get the proxy server running.

Leave this terminal window open to keep the proxy server running.

Make sure you turn off the server when you are done

Note: If you are just writing a sample app it’s okay to interact directly with a single database. But for production apps you will always want two database instances (at minimum) — one for production and one for development. This will separate the horrible things you and your team do in development from the data your users will interact with.

You can follow the same directions to create another instance on Google Cloud. Make sure you update the activation shell script and the sql proxy tool to connect to the dev database if this is your case.

Database Connection: Django Development Server

This one is simple. With the proxy server running, activate your environment (which will load the shell script). Then you can spin up your dev server using ./manage.py runserver and interact with your database instance.

Database Connection: PostgreSQL [psql] Client

If you want to muck around with the database directly you can connect using the psql client and the proxy server that is exposing the connection to your instance.

psql “host=127.0.0.1 port=3306 sslmode=disable dbname=DATABASE-NAME user=USERNAME”