In this blog post, you’ll learn how to connect Django to a PostgreSQL database hosted on Google Cloud SQL.
This post is part of the Dockerized Django Back-end API with Angular Front-end Tutorial. Check out all the parts of the tutorial there.
In the last part of the tutorial, we’ve learned how to build a Docker Image from a private GitHub or Bitbucket repository in Google Cloud Build.
To get the code to where we left off in the last blog post, use:
$ git checkout v1.18
We’re almost ready to deploy our Django REST API in production on GCP!
If you recall from part 9 of the tutorial, we said that in production we’ll use a Postgres database hosted on Google’s Cloud SQL service.
Hence, we left the DATABASES
field of the settings_prod.py
file the same as the one from settings_dev.py
which of course wouldn’t work in production.
Let’s proceed to create a production PostgreSQL database for our Django API.
Creating a PostgreSQL Instance in Cloud SQL
We’ll follow the documentation notes here to create a PostgreSQL instance in Cloud SQL.
-
Go to the Cloud SQL Instances page.
-
Click Create instance.
-
Select PostgreSQL and click Next.
-
Enter a name. Do not include sensitive or personally identifiable information in your instance name as it is externally visible.
-
Enter a password for the postgres
user.
-
Configure instance settings under Configuration options.
For Connectivity, select Public IP unless you want to do a more complex setup.
See the documentation notes linked above for details.
- Click Create.
Creating a Database and User for Django
Next, we’ll use the psql
client to create the Django database and user.
-
In the GCP Console, open Cloud Shell.
-
Use the built-in psql
client to connect to your Cloud SQL instance:
$ gcloud sql connect [INSTANCE_ID] --user=postgres
-
Enter the password for the postgres
user you’ve set in the previous section.
-
Now, from the psql
command-line, create a database for Django:
$ CREATE DATABASE my_django_db;
- Create a Django user and password:
$ CREATE USER my_django_user WITH PASSWORD 'password_for_my_django_user';
Of course, choose a good password or use this list for suggestions.
- Set the user’s client encoding to UTF-8:
$ ALTER ROLE my_user SET client_encoding TO 'utf8';
- Grant the Django user rights over the Django database:
$ GRANT ALL PRIVILEGES ON DATABASE my_django_db TO my_django_user;
Replace names as appropriate.
Adding the Database Details to settings_prod.py
Now that we have our Django database and user, we could just add them to our Django settings file, specifically to settings_prod.py
in the django/todoproj
directory.
For security and convenience purposes, instead of adding these details to the repository, let’s use environment variables instead:
We’ll set these environment variables in the next blog post when we’ll deploy our container on a Google Compute Engine Virtual Machine (VM).
Given the changes to the production settings file, you should now rebuild the Docker image.
And that’s about it for this part of the tutorial!
The only code change in the repo is using environment variables for the DATABASE
field. See my git commit or use:
$ git checkout v1.19
Summary
In this part of the tutorial, we’ve learned how to connect our Django API to a PostgreSQL database hosted on Google Cloud SQL. We’ve covered creating a Cloud SQL instance, the Django database, and the Django user.
In the next part of the tutorial, we’ll see how to use our Docker image to deploy a container on a Google Compute Engine VM.
Credit: For this tutorial, I’ve used the following resources: