Django with PostgreSQL in Cloud SQL

Connecting Django to a PostgreSQL Database in Cloud SQL (Part 13)

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.

  1. Go to the Cloud SQL Instances page.

  2. Click Create instance.

  3. Select PostgreSQL and click Next.

  4. Enter a name. Do not include sensitive or personally identifiable information in your instance name as it is externally visible.

  5. Enter a password for the postgres user.

  6. 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.

  1. Click Create.

Creating a Database and User for Django

Next, we’ll use the psql client to create the Django database and user.

  1. In the GCP Console, open Cloud Shell.

  2. Use the built-in psql client to connect to your Cloud SQL instance:

$ gcloud sql connect [INSTANCE_ID] --user=postgres
  1. Enter the password for the postgres user you’ve set in the previous section.

  2. Now, from the psql command-line, create a database for Django:

$ CREATE DATABASE my_django_db;
  1. 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.

  1. Set the user’s client encoding to UTF-8:
$ ALTER ROLE my_user SET client_encoding TO 'utf8';
  1. 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:

About the Author Dragos Stanciu

follow me on:

Subscribe

Like this article? Stay updated by subscribing to my weekly newsletter:

Leave a Comment: