Skip to main content
This tutorial shows you how build a simple Rust application with CockroachDB and the Rust-Postgres driver.

Before you begin

You must have Rust and Cargo installed. For instructions on installing Rust and Cargo, see the Cargo documentation.

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 connection string

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 the General connection string section, then copy the connection string provided and save it in a secure location. The sample application used in this tutorial uses system CA certificates for server certificate verification, so you can skip the Download CA Cert instructions.
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:
The project has the following structure:
The Cargo.toml file is the configuration file for the example, and sets the dependencies for the project.
The main function is the entry point for the application, with the code for connecting to the cluster, creating the accounts table, creating accounts in that table, and transferring money between two accounts. The execute_txn function wraps database operations in the context of an explicit transaction. If a is thrown, the function will retry committing the transaction, with exponential backoff, until the maximum number of retries is reached (by default, 15).
CockroachDB may require the in case of read/write . CockroachDB provides a generic retry function that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code.
The transfer_funds function calls execute_txn to perform the actual transfer of funds from one account to the other.

Step 3. Run the code

  1. In a terminal go to the example-app-rust-postgres directory.
  2. Set the DATABASE_URL environment variable to the connection string to your CockroachDB Cloud cluster:
    1. Edit the connection string you copied earlier and replace sslmode=verify-full with sslmode=require.
You must change the sslmode in your connection string to sslmode=require, as the Rust postgres driver does not recognize sslmode=verify-full. This example uses postgres-openssl, which will perform host verification when the sslmode=require option is set, so require is functionally equivalent to verify-full.
For example:
  1. Set the DATABASE_URL environment variable to the modified connection string.
    Where is the modified connection string.
The app uses the connection string saved to the DATABASE_URL environment variable to connect to your cluster and execute the code.
  1. Run the code to create a table and insert some rows:
    The output should look similar to the following:

What’s next?

Read more about using the Rust-Postgres driver. You might also be interested in the following pages: