Step 1. Install prerequisites
- Install the latest version of .
-
Install the Python psycopg2 driver:
pip install psycopg2 -
Install the Python Requests library:
pip install requests - Install the latest version of .
-
Install the latest version of Go:
brew install go -
Install the PostgreSQL driver:
go get github.com/lib/pq
Step 2. Start a single-node cluster
For the purpose of this tutorial, you need only one CockroachDB node running in insecure mode, so use the command in the foreground:Step 3. Create a user
In a new terminal window, open the and create a new SQL user,maxroach:
Step 4. Create a database and grant privileges
Next, create a database calledjsonb_test:
maxroach user:
Step 5. Create a table
Still in the SQL shell, create a table calledprogramming:
Step 6. Run the code
Now that you have a database, a SQL user, and a table, let’s run code to insert rows into the table. Download thejson-sample.py file, or create the file yourself and copy the code into it:
"after" string that tells you how to get the next page. Therefore, the program does the following in a loop:
- Makes a request to the API.
- Grabs the
"after"string. - Inserts the results into the table.
- Uses the new
"after"string as the basis for the next request.
json-sample.go file, or create the file yourself and copy the code into it:
"after" string that tells you how to get the next page. Therefore, the program does the following in a loop:
- Makes a request to the API.
- Inserts the results into the table and grabs the
"after"string. - Uses the new
"after"string as the basis for the next request.
Step 7. Query the data
Back in the terminal where the SQL shell is running, verify that rows of data are being inserted into your table:Since you are querying live data, your results for this and the following steps may vary from the results documented in this tutorial.
Step 8. Create a GIN index to optimize performance
The query in the previous step took 103.748ms. To optimize the performance of queries that filter on theJSONB column, let’s create a on the column:
Step 9. Run the query again
Now that there is a GIN index, the same query will run much faster:Step 10. Clean up
If the program is still running, pressctrl-c to terminate it.
Get the process ID of the node:

