Integrate a managed CockroachDB serverless database to your application and benefit from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network with zero configuration.
CockroachDB
This guide explains how to connect a CockroachDB serverless database to an application running on Koyeb. To successfully follow this documentation, you will need to have:
The application that connects to the CockroachDB database uses Prisma as an ORM. Prisma is an open-source ORM. We use it to synchronize our database schema with our Prisma schema.
If you already have a freshly created CockroachDB database running and want to quickly preview how to connect your database to an application running on Koyeb, use the Deploy to Koyeb button below.
Make sure to replace the DATABASE_URL
environment variable with the connection string of your CockroachDB database.
To create a CockroachDB database,
sign into your CockroachDB account
On the CockroachDB control panel, start by clicking the Create Cluster button to provision a cluster and access the database creation page.
cockroachdb-koyeb
When you are done configuring your database, click the Create your free cluster button. Your cluster will be provisioned.
On your CockroachDB cluster page, click on the Connection tab to create a new user and password. Save that information in a safe place. Under General connection string you will find the connection string you can use to connect to your cluster. The connection string should be similar to the following:
postgres://<user>:<password>@<host>:<port>/defaultdb?sslmode=verify-full
Save it in a safe place, we will need it in the next step to connect our application to the database.
You can deploy on Koyeb using the control panel or via the Koyeb CLI.
To deploy the example application using the
control panel
github.com/koyeb/example-express-prisma
as the GitHub repository to
deploy and keep the default branch main
npm run cockroachdb:init
as the Build commandDATABASE_URL
environment variable to indicate the application how to
connect to the CockroachDB. Give the environment variable a name,
DATABASE_URL
and enter the connection string from earlier as the
valueexpress-cockroachdb
Koyeb is now building the application. Once the build has finished, you will be able
to access your application running on Koyeb by clicking the URL ending with
.koyeb.app
.
The example application exposes a /planets
endpoint that you can use to list
planets from the database. Once your deployment is live, you should see the
following results when navigating to https://<YOUR_APP_URL>.koyeb.app/planets
:
[ { "id": "827696777012871169", "name": "Mercury" }, { "id": "827696777225666561", "name": "Venus" }, { "id": "827696777389539329", "name": "Mars" } ]
To deploy the example application using the Koyeb CLI, run the following command in your terminal:
koyeb app init express-cockroachdb \ --git github.com/koyeb/example-express-prisma \ --git-build-command "npm run cockroachdb:init" \ --git-branch main \ --ports 8080:http \ --routes /:8080 \ --env PORT=8080 \ --env DATABASE_URL="postgresql://<user>:<password>@<host>:<port>/defaultdb?sslmode=verify-full"
Make sure to replace user
, password
, host
, and port
with your own.
Access deployment logs
To track the app deployment and visualize build logs, execute the following command:
koyeb service logs express-cockroachdb/express-cockroachdb -t build
Access your app
Once the deployment of your application has finished, you can retrieve the public domain to access your application by running the following command:
$ koyeb app get express-cockroach ID NAME STATUS DOMAINS CREATED AT b60982b7 express-cockroachdb STARTING ["express-cockroachdb-ed.koyeb.app"] 01 Dec 22 15:06 UTC
The example application exposes a /planets
endpoint that you can use to list
planets from the database. Once your deployment is live, you should see the
following results when navigating to https://<YOUR_APP_URL>.koyeb.app/planets
:
[ { "id": "827696777012871169", "name": "Mercury" }, { "id": "827696777225666561", "name": "Venus" }, { "id": "827696777389539329", "name": "Mars" } ]
To delete the example application and the Cockroach database and avoid incurring any charges, follow these steps: