Connect your database
Connecting 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:
prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
In this case, the url
is set via an environment variable which is defined in .env
:
.env
DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
You now need to adjust the connection URL to point to your own database.
Connection URL
The format of the connection URL for your database typically depends on the database you use. For MySQL, it looks as follows (the parts spelled all-uppercased are placeholders for your specific connection details):
mysql://USER:PASSWORD@HOST:PORT/DATABASE
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 (typically3306
for MySQL)DATABASE
: The name of the database
As an example, for a MySQL database hosted on AWS RDS, the connection URL might look similar to this:
.env
DATABASE_URL="mysql://johndoe:XXX@mysql–instance1.123456789012.us-east-1.rds.amazonaws.com:3306/mydb"
When running MySQL locally, your connection URL typically looks similar to this:
.env
DATABASE_URL="mysql://root:randompassword@localhost:3306/mydb"