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 code
Clone the code’s GitHub repo:requirements.txt file includes the required libraries to connect to CockroachDB with SQLAlchemy, including the sqlalchemy-cockroachdb Python package, which accounts for some differences between CockroachDB and PostgreSQL:
dbinit.sql file initializes the database schema that the application uses:
models.py uses SQLAlchemy to map the Accounts table to a Python object:
main.py uses SQLAlchemy to map Python methods to SQL operations:
main.py also executes the main method of the program.
Step 3. Install the application requirements
This tutorial usesvirtualenv for dependency management.
-
Install
virtualenv: -
At the top level of the app’s project directory, create and then activate a virtual environment:
-
Install the required modules to the virtual environment:
Step 4. Initialize the database
-
Set the
DATABASE_URLenvironment variable to the connection string for your cluster:Where is the connection string you copied earlier. -
To initialize the example database, use the command to execute the SQL statements in the
dbinit.sqlfile:The SQL statement in the initialization file should execute: - 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 code
Clone the code’s GitHub repo:requirements.txt file includes the required libraries to connect to CockroachDB with SQLAlchemy, including the sqlalchemy-cockroachdb Python package, which accounts for some differences between CockroachDB and PostgreSQL:
dbinit.sql file initializes the database schema that the application uses:
models.py uses SQLAlchemy to map the Accounts table to a Python object:
main.py uses SQLAlchemy to map Python methods to SQL operations:
main.py also executes the main method of the program.
Step 3. Install the application requirements
This tutorial usesvirtualenv for dependency management.
-
Install
virtualenv: -
At the top level of the app’s project directory, create and then activate a virtual environment:
-
Install the required modules to the virtual environment:
Step 4. Initialize the database
-
Set the
DATABASE_URLenvironment variable to the connection string for your cluster: -
To initialize the example database, use the command to execute the SQL statements in the
dbinit.sqlfile:The SQL statement in the initialization file should execute:
Step 5. Run the code
main.py uses the connection string saved to the DATABASE_URL environment variable to connect to your cluster and execute the code.
The example application uses the general connection string, which begins with
postgresql:// but modifies it so it uses the cockroachdb:// prefix. It does this so SQLAlchemy will use the CockroachDB SQLAlchemy adapter.Best practices
Use the run_transaction function
We strongly recommend using the sqlalchemy_cockroachdb.run_transaction() function as shown in the code samples on this page. This abstracts the details of away from your application code. Transaction retries are more frequent in CockroachDB than in some other databases because we use optimistic concurrency control rather than locking. Because of this, a CockroachDB transaction may have to be tried more than once before it can commit. This is part of how we ensure that our transaction ordering guarantees meet the ANSI SERIALIZABLE isolation level.
In addition to the above, using run_transaction has the following benefits:
- Because it must be passed a sqlalchemy.orm.session.sessionmaker object ( not a session ), it ensures that a new session is created exclusively for use by the callback, which protects you from accidentally reusing objects via any sessions created outside the transaction.
- It abstracts away the from your application, which keeps your application code portable across different databases. For example, the sample code given on this page works identically when run against PostgreSQL (modulo changes to the prefix and port number in the connection string).
Avoid mutations of session and/or transaction state inside run_transaction()
In general, this is in line with the recommendations of the SQLAlchemy FAQs, which state (with emphasis added by the original author) that
As a general rule, the application should manage the lifecycle of the session externally to functions that deal with specific data. This is a fundamental separation of concerns which keeps data-specific operations agnostic of the context in which they access and manipulate that data.and
Keep the lifecycle of the session (and usually the transaction) separate and external.In keeping with the above recommendations from the official docs, we strongly recommend avoiding any explicit mutations of the transaction state inside the callback passed to
run_transaction, since that will lead to breakage. Specifically, do not make calls to the following functions from inside run_transaction:
sqlalchemy.orm.Session.commit()(or other variants ofcommit()): This is not necessary becausecockroachdb.sqlalchemy.run_transactionhandles the savepoint/commit logic for you.sqlalchemy.orm.Session.rollback()(or other variants ofrollback()): This is not necessary becausecockroachdb.sqlalchemy.run_transactionhandles the commit/rollback logic for you.Session.flush(): This will not work as expected with CockroachDB because CockroachDB does not support nested transactions, which are necessary forSession.flush()to work properly. If the call toSession.flush()encounters an error and aborts, it will try to rollback. This will not be allowed by the currently-executing CockroachDB transaction created byrun_transaction(), and will result in an error message like the following:sqlalchemy.orm.exc.DetachedInstanceError: Instance <FooModel at 0x12345678> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: http://sqlalche.me/e/bhk3).
Break up large transactions into smaller units of work
If you see an error message liketransaction is too large to complete; try splitting into pieces, you are trying to commit too much data in a single transaction. As described in our docs, the size limit for transactions is defined by the kv.transaction.max_intents_bytes setting, which defaults to 256 KiB. Although this setting can be changed by an admin, we strongly recommend against it in most cases.
Instead, we recommend breaking your transaction into smaller units of work (or “chunks”). A pattern that works for inserting large numbers of objects using run_transaction to handle retries automatically for you is shown below.
Use IMPORT INTO to read in large data sets
If you are trying to get a large data set into CockroachDB all at once (a bulk import), avoid writing client-side code that uses an ORM and use the statement instead. It is much faster and more efficient than making a series of and such as are generated by calls to session.bulk_save_objects().
For more information about importing data from PostgreSQL, see .
For more information about importing data from MySQL, see .
Prefer the query builder
In general, we recommend using the query-builder APIs of SQLAlchemy (e.g.,Engine.execute()) in your application over the Session/ORM APIs if at all possible. That way, you know exactly what SQL is being generated and sent to CockroachDB, which has the following benefits:
- It’s easier to debug your SQL queries and make sure they are working as expected.
- You can more easily tune SQL query performance by issuing different statements, creating and/or using different indexes, etc. For more information, see .
Joins without foreign keys
SQLAlchemy relies on the existence of to generate from your application code. If you remove foreign keys from your schema, SQLAlchemy will not generate joins for you. As a workaround, you can create a “custom foreign condition” by adding arelationship field to your table objects, or do the equivalent work in your application.
See also
- The SQLAlchemy docs
- Transactions

