<!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG-spv-->{"id":1997676,"date":"2021-09-26T19:48:00","date_gmt":"2021-09-26T17:48:00","guid":{"rendered":"http:\/\/nhub.news\/?p=1997676"},"modified":"2021-09-26T23:03:37","modified_gmt":"2021-09-26T21:03:37","slug":"how-to-use-cockroachdb-with-your-django-application","status":"publish","type":"post","link":"http:\/\/nhub.news\/ru\/2021\/09\/how-to-use-cockroachdb-with-your-django-application\/","title":{"rendered":"How To Use CockroachDB With Your Django Application"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>Looking for more information about how to use CockroachDB with Django? Check out this step-by-step tutorial to learn more.<\/b><br \/>\nThis tutorial is intended to be a quick ramp-up on CockroachDB with Django. In case you&#8217;re searching for a proper Django tutorial, this is not it. At the time of writing, django-cockroachdb library is available in two versions, (2 and 3). This tutorial will cover version 3, and is inspired by the Digital Ocean tutorial using Django with PostgreSQL. I am going to highlight the steps where this tutorial differs from the original. For everything else, we will assume the tutorial is followed as is. I originally wrote this post two years ago and had since updated it as CockroachDB RBAC is no longer an enterprise-only feature so we can skip that step. I&#8217;m also including steps to launch a Docker instance to make this more portable and comprehensive. I am going to run a Docker container with Ubuntu and expose port 8080 for the DB Console, port 26257 for the SQL shell, and port 8000 for my Django application. I will install CockroachDB and run the Django app inside the container. In a terminal, execute the following command to launch this container: docker run -d -p 8080:8080 -p 26257:26257 -p 8000:8000 &#8212;name django -i -t ubuntu \/bin\/bash Then execute the following command to connect to the interactive shell inside this container: docker exec -it django bash The first thing we want to do is install some necessary dependencies. apt-get update &amp;&amp; apt-get install python3-pip python3-dev libpq-dev vim curl wget virtualenv -y Then follow these instructions to install CockroachDB. Once installed, we can launch a single node cluster. cockroach start-single-node &#8212;insecure &#8212;background You should be able to access the DB Console on your host at http:\/\/localhost:8080. cockroach sql &#8212;insecure Create a database for the project: CREATE DATABASE myproject; Set the desired database as default: SET DATABASE = myproject; Since we\u2019re using CRDB in insecure mode, the password field is empty. Create the Django project user. CREATE USER myprojectuser; Create a role for our Django app. CREATE ROLE django; Verify role is created. Grant privileges to the Django role you created. GRANT CREATE, INSERT, UPDATE, SELECT ON DATABASE myproject TO django; Add myprojectuser to Django role. GRANT django TO myprojectuser; Now you can verify access to CRDB using newly created user. &gt;\\q cockroach sql &#8212;insecure &#8212;database=myproject &#8212;user=myprojectuser Now we\u2019re ready to install django and cockroachdb library. pip3 install django psycopg2 django-cockroachdb We are ready to create a Django project. django-admin startproject myproject. Here, you will populate CRDB properties instead of PostgreSQL. This is a sample configuration. In case you want something more secure, the pypi page for the django_cockroachdb adapter has samples for SSL. Also, check out my own docker compose repo in case you want a sample that works with Kerberos authentication. python3 manage.py makemigrations python3 manage.py migrate At this point you should see some new Django databases created in the cluster. python3 manage.py createsuperuser python3 manage.py runserver 0.0.0.0:8000 Navigate to http:\/\/localhost:8000\/ on your host. This concludes the tutorial. You can stop the container and remove it by issuing the following commands: docker kill django docker container rm django We&#8217;ve come a long way since 2019 and a lot of things are much simpler now. We no longer need an enterprise license to take advantage of SQL roles and we are much better suited working with third party libraries like Django.<\/p>\n<script>jQuery(function(){jQuery(\".vc_icon_element-icon\").css(\"top\", \"0px\");});<\/script><script>jQuery(function(){jQuery(\"#td_post_ranks\").css(\"height\", \"10px\");});<\/script><script>jQuery(function(){jQuery(\".td-post-content\").find(\"p\").find(\"img\").hide();});<\/script>","protected":false},"excerpt":{"rendered":"<p>Looking for more information about how to use CockroachDB with Django? Check out this step-by-step tutorial to learn more. This tutorial is intended to be a quick ramp-up on CockroachDB with Django. In case you&#8217;re searching for a proper Django tutorial, this is not it. At the time of writing, django-cockroachdb library is available in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1997675,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts\/1997676"}],"collection":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/comments?post=1997676"}],"version-history":[{"count":1,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts\/1997676\/revisions"}],"predecessor-version":[{"id":1997677,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts\/1997676\/revisions\/1997677"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/media\/1997675"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/media?parent=1997676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/categories?post=1997676"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/tags?post=1997676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}