Configure Django Application in Cloud SQL with PostgreSQL

Parth Vijay
3 min readJul 6, 2020

Before you get started, you must login to your Google Cloud Platform and set up the following items in Google Cloud.

  1. In the Cloud Console, on the project selector page, select or create a Cloud project.
  2. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

Creating and managing PostgreSQL databases

Creating instances:

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.
  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; it is externally visible.

You do not need to include the project ID in the instance name. This is done automatically where appropriate (for example, in the log files).

Note: You cannot reuse an instance name for up to a week after you have deleted the instance.

5. Enter a password for the postgres user.

6. Under Location, select the region and zone for your instance.

Place your instance in the same region as the resources that access it. The region you select can’t be modified in the future. In most cases, you don’t need to specify a zone.

7. Click Create.

Creating and managing PostgreSQL users

To create a user:

1. Go to the Cloud SQL Instances page in the Google Cloud Console.

2. Select the instance to open its Overview page.

3. Select Users from the Navigation menu.

4. Click Create user account.

5. In the Create user account dialog, specify:

  • A User name
  • A Password

6. Click Create.

Users created using Cloud SQL have the privileges associated with the cloud sql superuser role: CREATEROLE, CREATEDB, and LOGIN. If you need to change the attributes for a user, use the ALTER ROLE command in the psql client. Not all attributes can be modified with ALTER ROLE. Exceptions include the NOSUPERUSER and NOREPLICATION roles.

Creating a database

To create a database:

1. Go to the Cloud SQL Instances page in the Google Cloud Console

2. Select the instance PostgreSQL.

3. Select the DATABASES tab.

4. Click Create database.

5. In the New database dialog, specify the name of the database.

6. Click Create.

Configuring public IP connectivity

You can configure your Cloud SQL instance to have a public IPv4 address, and to accept connections from specific IP addresses or a range of addresses by adding authorized addresses to your instance.

If you configure your instance to accept connections via its public IP address, you should also configure it to use SSL to keep your data secure. For more information, see:

Configure SSL for Instanceshttps://cloud.google.com/sql/docs/postgres/configure-ssl-instance

To enable public IP and add an authorized address:

When you enable public IP for your instance, it is configured with a public, static IPv4 address.

1. Go to the Cloud SQL Instances page in the Google Cloud Console.

2. Click the instance name to open its Instance details page.

3. Select the Connections tab.

4. Select the Public IP checkbox.

5. Click Add network.

6. In the Network field, enter the IP address or address range you want to allow connections from.

Use CIDR notation.

Check your Public IP from here- https://www.whatismyip.com/my-ip-information/

7. Optionally, enter a name for this entry.

8. Click Done.

9. Click Save to update the instance.

Change settings.py of your Django project

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.postgresql_psycopg2',        'HOST': '[Your-Cloud-SQL-Public-IP]',        'USER': '[YOUR-USERNAME]',        'PASSWORD': '[YOUR-PASSWORD]',        'NAME': '[YOUR-DATABASE]',    }}

Install necessary library for **PostgreSQL** database

pip3 install psycopg2-binary

Thank You, Happy Coding!!!

--

--