TypeScript
SQL Server

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
1datasource db {
2 provider = "postgresql"
3 url = env("DATABASE_URL")
4}

In this case, the url is set via an environment variable which is defined in .env:

.env
1DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"

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. For PostgreSQL, it looks as follows (the parts spelled all-uppercased are placeholders for your specific connection details):

postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA

Note: In most cases, you can use the postgres:// and postgresql:// URI scheme designators interchangeably - however, depending on how your database is hosted, you might need to be specific.

If you're unsure what to provide for the schema parameter for a PostgreSQL connection URL, you can probably omit it. In that case, the default schema name public will be used.

As an example, for a PostgreSQL database hosted on Heroku, the connection URL might look similar to this:

.env
1DATABASE_URL="postgresql://opnmyfngbknppm:XXX@ec2-46-137-91-216.eu-west-1.compute.amazonaws.com:5432/d50rgmkqi2ipus?schema=hello-prisma"

When running PostgreSQL locally on Mac OS, your user and password as well as the database name typically correspond to the current user of your OS, e.g. assuming the user is called janedoe:

.env
1DATABASE_URL="postgresql://janedoe:janedoe@localhost:5432/janedoe?schema=hello-prisma"
prisma/schema.prisma
1datasource db {
2 provider = "postgresql"
3 url = env("DATABASE_URL")
4}

Note that the default schema created by prisma init uses PostgreSQL, so you first need to switch the provider to mysql:

prisma/schema.prisma
1datasource db {
provider = "mysql"
3 url = env("DATABASE_URL")
4}

In this case, the url is set via an environment variable which is defined in .env:

.env
1DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"

You now need to adjust the connection URL to point to your own database.

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 user
  • PASSWORD: The password for your database user
  • PORT: The port where your database server is running (typically 3306 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
1DATABASE_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
1DATABASE_URL="mysql://root:randompassword@localhost:3306/mydb"
prisma/schema.prisma
1datasource db {
2 provider = "sqlserver"
3 url = env("DATABASE_URL")
4}

The url is set via an environment variable, the following example connection URL uses SQL authentication, but there are other ways to format your connection URL

prisma/.env
1DATABASE_URL="sqlserver://localhost:1433;database=mydb;user=sa;password=r@ndomP@$$w0rd;trustServerCertificate=true"

Adjust the connection URL to match your setup - see Microsoft SQL Server connection URL for more information.

Make sure TCP/IP connections are enabled via SQL Server Configuration Manager to avoid No connection could be made because the target machine actively refused it. (os error 10061)

Edit this page on GitHub