Skip to main content
This tutorial shows you how build a simple Python application with CockroachDB and the Django framework. CockroachDB supports Django versions 3.1+.
The example code and instructions on this page use Python 3.9 and Django 3.1.

Step 1. Start CockroachDB

Choose your installation method

You can create a CockroachDB Basic cluster using either the CockroachDB Cloud Console, a web-based graphical user interface (GUI) tool, or ccloud, a command-line interface (CLI) tool.

Create a free cluster

Organizations without billing information on file can only create one CockroachDB Basic cluster.
  1. If you haven’t already, sign up for a CockroachDB Cloud account.
  2. Log in to your CockroachDB Cloud account.
  3. On the Clusters page, click Create cluster.
  4. On the Select a plan page, select Basic.
  5. On the Cloud & Regions page, select a cloud provider (GCP or AWS) in the Cloud provider section.
  6. In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Basic clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
  7. Click Next: Capacity.
  8. On the Capacity page, select Start for free. Click Next: Finalize.
  9. On the Finalize page, click Create cluster. Your cluster will be created in a few seconds and the Create SQL user dialog will display.

Create a SQL user

The Create SQL user dialog allows you to create a new SQL user and password.
  1. Enter a username in the SQL user field or use the one provided by default.
  2. Click Generate & save password.
  3. Copy the generated password and save it in a secure location.
  4. Click Next. Currently, all new SQL users are created with admin privileges. For more information and to change the default settings, see .

Get the root certificate

The Connect to cluster dialog shows information about how to connect to your cluster.
  1. Select General connection string from the Select option dropdown.
  2. Open a new terminal on your local machine, and run the CA Cert download command provided in the Download CA Cert section. The client driver used in this tutorial requires this certificate to connect to CockroachDB Cloud.

Get the connection string

Open the General connection string section, then copy the connection string provided and save it in a secure location.
The connection string is pre-populated with your username, password, cluster name, and other details. Your password, in particular, will be provided only once. Save it in a secure place (Cockroach Labs recommends a password manager) to connect to your cluster in the future. If you forget your password, you can reset it by going to the SQL Users page for the cluster, found at https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users.

Step 2. Get the sample code

Clone the code’s GitHub repo:
The project directory structure should look like this:

Step 3. Install the application requirements

To use CockroachDB with Django, the following modules are required:
The major version of django-cockroachdb must correspond to the major version of django. The minor release numbers do not need to match.
The requirements.txt file at the top level of the example-app-python-django project directory contains a list of the requirements needed to run this application:
This tutorial uses virtualenv for dependency management.
  1. Install virtualenv:
  2. At the top level of the app’s project directory, create and then activate a virtual environment:
  3. Install the modules listed in requirements.txt to the virtual environment:

Step 4. Build out the application

Configure the database connection

Open cockroach_example/cockroach_example/settings.py, and configure the DATABASES dictionary to connect to your cluster using the connection parameters that you copied earlier.
For more information about configuration a Django connection to CockroachDB Standard, see Connect to a CockroachDB Cluster. After you have configured the app’s database connection, you can start building out the application.

Models

Start by building some models, defined in a file called models.py. You can copy the sample code below and paste it into a new file, or you can download the file directly.
In this file, we define some simple classes that map to the tables in the cluster.

Views

Next, build out some class-based views for the application in a file called views.py. You can copy the sample code below and paste it into a new file, or you can download the file directly.
This file defines the application’s views as classes. Each view class corresponds to one of the table classes defined in models.py. The methods of these classes define read and write transactions on the tables in the database. Importantly, the file defines a in the decorator function retry_on_exception(). This function decorates each view method, ensuring that transaction ordering guarantees meet the ANSI SERIALIZABLE isolation level. For more information about how transactions (and retries) work, see .

URL routes

Lastly, define some URL routes in a file called urls.py. You can copy the sample code below and paste it into the existing urls.py file, or you can download the file directly and replace the existing one.

Step 5. Initialize the database

In the top cockroach_example directory, use the manage.py script to create Django migrations that initialize the database for the application:
This initializes the tables defined in models.py, in addition to some other tables for the admin functionality included with Django’s starter application.

Step 6. Run the app

  1. In a different terminal, navigate to the top of the cockroach_example directory, and start the app:
    The output should look like this:
    To perform simple reads and writes to the database, you can send HTTP requests to the application server listening at http://0.0.0.0:8000/.
  2. In a new terminal, use curl to send a POST request to the application:
    This request inserts a new row into the cockroach_example_customers table.
  3. Send a GET request to read from the cockroach_example_customers table:
    You can also query the table directly in the to see the changes:
  4. Enter Ctrl+C to stop the application.

What’s next?

Read more about writing a Django app. You might also be interested in the following pages: