The example code and instructions on this page use Python 3.9 and Django 3.1.
Step 1. Start CockroachDB
- Use CockroachDB Cloud
- Use a Local Cluster
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, orccloud, a command-line interface (CLI) tool.- Cloud Console (GUI)
- ccloud CLI
Create a free cluster
Organizations without billing information on file can only create one CockroachDB Basic cluster.
- If you haven’t already, sign up for a CockroachDB Cloud account.
- Log in to your CockroachDB Cloud account.
- On the Clusters page, click Create cluster.
- On the Select a plan page, select Basic.
- On the Cloud & Regions page, select a cloud provider (GCP or AWS) in the Cloud provider section.
- 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.
- Click Next: Capacity.
- On the Capacity page, select Start for free. Click Next: Finalize.
- 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.- Enter a username in the SQL user field or use the one provided by default.
- Click Generate & save password.
- Copy the generated password and save it in a secure location.
- 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.- Select General connection string from the Select option dropdown.
- 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:Step 3. Install the application requirements
To use CockroachDB with Django, the following modules are required:djangopsycopg2(recommended for production environments) orpsycopg2-binary(recommended for development and testing).django-cockroachdb
The major version of
django-cockroachdb must correspond to the major version of django. The minor release numbers do not need to match.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:
virtualenv for dependency management.
-
Install
virtualenv: -
At the top level of the app’s project directory, create and then activate a virtual environment:
-
Install the modules listed in
requirements.txtto the virtual environment:
Step 4. Build out the application
Configure the database connection
Opencockroach_example/cockroach_example/settings.py, and configure the DATABASES dictionary to connect to your cluster using the connection parameters that you copied earlier.
- If you haven’t already, .
-
Run the command:
This starts an insecure, single-node cluster.
-
Take note of the following connection information in the SQL shell welcome text:
You’ll use the
sqlconnection string to connect to the cluster later in this tutorial.
The
--insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.Step 2. Get the sample code
Clone the code’s GitHub repo:Step 3. Install the application requirements
To use CockroachDB with Django, the following modules are required:djangopsycopg2(recommended for production environments) orpsycopg2-binary(recommended for development and testing).django-cockroachdb
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:
virtualenv for dependency management.
-
Install
virtualenv: -
At the top level of the app’s project directory, create and then activate a virtual environment:
-
Install the modules listed in
requirements.txtto the virtual environment:
Step 4. Build out the application
Models
Start by building some models, defined in a file calledmodels.py. You can copy the sample code below and paste it into a new file, or you can download the file directly.
Views
Next, build out some class-based views for the application in a file calledviews.py. You can copy the sample code below and paste it into a new file, or you can download the file directly.
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 calledurls.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 topcockroach_example directory, use the manage.py script to create Django migrations that initialize the database for the application:
models.py, in addition to some other tables for the admin functionality included with Django’s starter application.
Step 6. Run the app
-
In a different terminal, navigate to the top of the
cockroach_exampledirectory, 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 athttp://0.0.0.0:8000/. -
In a new terminal, use
curlto send a POST request to the application:This request inserts a new row into thecockroach_example_customerstable. -
Send a GET request to read from the
cockroach_example_customerstable:You can also query the table directly in the to see the changes: - Enter Ctrl+C to stop the application.

