Connect your database
To connect your database, you need to set the url
field of the datasource
block in your Prisma schema to your database connection URL:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Note that the default schema created by prisma init
uses PostgreSQL as the provider
. For CockroachDB, you need to edit the datasource
block to use the cockroachdb
provider instead:
datasource db {
provider = "cockroachdb"
url = env("DATABASE_URL")
}
The url
is set via an environment variable which is defined in .env
. You now need to adjust the connection URL to point to your own database.
The format of the connection URL for your database depends on the database you use. CockroachDB uses the PostgreSQL connection URL format, which has the following structure (the parts spelled all-uppercased are placeholders for your specific connection details):
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?PARAMETERS
Here's a short explanation of each component:
USER
: The name of your database userPASSWORD
: The password for your database userPORT
: The port where your database server is running. The default for CockroachDB is26257
.DATABASE
: The name of the databasePARAMETERS
: Any additional connection parameters. See the CockroachDB documentation here.
For a CockroachDB Serverless or Cockroach Dedicated database hosted on CockroachDB Cloud, the connection URL looks similar to this:
DATABASE_URL="postgresql://<myusername>:<mypassword>@<short-id>.<region>.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full&sslrootcert=$HOME/.postgresql/root.crt&options=--<mycluster>"
To find your connection string on CockroachDB Cloud, click the 'Connect' button on the overview page for your database cluster, and select the 'Connection string' tab.
For a CockroachDB database hosted locally, the connection URL looks similar to this:
DATABASE_URL="postgresql://root@localhost:26257?sslmode=disable"
Your connection string is displayed as part of the welcome text when starting CockroachDB from the command line.