This tutorial shows you how build a simple C# application with CockroachDB and the .NET Npgsql driver.
Start CockroachDB
Choose whether to run a temporary local cluster or a free CockroachDB cluster on CockroachDB Serverless. The instructions below will adjust accordingly.
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 CockroachDB Cloud Regions 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.
Set up your cluster connection
The Connection info dialog shows information about how to connect to your cluster.
Click the Choose your OS dropdown, and select the operating system of your local machine.
Click the Connection string tab in the Connection info dialog.
Open a new terminal on your local machine, and run the command provided in step 1 to download the CA certificate. This certificate is required by some clients connecting to CockroachDB Cloud.
Copy the connection string provided in step 2 to a secure location.
Note: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.
- If you haven't already, download the CockroachDB binary.
Run the
cockroach democommand:$ cockroach demo \ --no-example-databaseThis starts a temporary, in-memory cluster and opens an interactive SQL shell to the cluster. Any changes to the database will not persist after the cluster is stopped.
Note:If
cockroach demofails due to SSL authentication, make sure you have cleared any previously downloaded CA certificates from the directory~/.postgresql.Take note of the
(sql)connection string in the SQL shell welcome text:# Connection parameters: # (webui) http://127.0.0.1:8080/demologin?password=demo76950&username=demo # (sql) postgres://demo:demo76950@127.0.0.1:26257?sslmode=require # (sql/unix) postgres://demo:demo76950@?host=%2Fvar%2Ffolders%2Fc8%2Fb_q93vjj0ybfz0fz0z8vy9zc0000gp%2FT%2Fdemo070856957&port=26257
Create a .NET project
In your terminal, run the following commands:
dotnet new console -o cockroachdb-test-app
cd cockroachdb-test-app
The dotnet command creates a new app of type console. The -o parameter creates a directory named cockroachdb-test-app where your app will be stored and populates it with the required files. The cd cockroachdb-test-app command puts you into the newly created app directory.
Install the Npgsql driver
Install the latest version of the Npgsql driver into the .NET project using the built-in nuget package manager:
dotnet add package Npgsql
Create a database
In the SQL shell, create the
bankdatabase that your application will use:> CREATE DATABASE bank;Create a SQL user for your app:
> CREATE USER <username> WITH PASSWORD <password>;Take note of the username and password. You will use it in your application code later.
Give the user the necessary permissions:
> GRANT ALL ON DATABASE bank TO <username>;
- If you haven't already, download the CockroachDB binary.
Start the built-in SQL shell using the connection string you got from the CockroachDB Cloud Console:
$ cockroach sql \ --url='<connection-string>'In the SQL shell, create the
bankdatabase that your application will use:> CREATE DATABASE bank;Exit the SQL shell:
> \q
Set the connection string
Choose your OS:
Set a DATABASE_URL environment variable to your connection string.
export DATABASE_URL="{connection string}"