Prisma schema reference
datasource
Defines a data source in the Prisma schema.
Fields
A datasource
block accepts the following fields:
Name | Required | Type | Description |
---|---|---|---|
provider | Yes | String (postgresql , mysql , sqlite , sqlserver , mongodb , cockroachdb ) | Describes which data source connectors to use. |
url | Yes | String (URL) | Connection URL including authentication info. Most connectors use the syntax provided by the database. |
shadowDatabaseUrl | No | String (URL) | Connection URL to the shadow database used by Prisma Migrate. Allows you to use a cloud-hosted database as the shadow database. |
directUrl | No | String (URL) | Connection URL for direct connection to the database. If you use a connection pooler URL in the url argument (for example, if you use Prisma Accelerate or pgBouncer), Prisma CLI commands that require a direct connection to the database use the URL in the directUrl argument. The directUrl property is supported by Prisma Studio from version 5.1.0 upwards. |
relationMode | No | String (foreignKeys , prisma ) | Sets whether referential integrity is enforced by foreign keys in the database or emulated in the Prisma Client. In preview in versions 3.1.1 and later. The field is named relationMode in versions 4.5.0 and later, and was previously named referentialIntegrity . |
extensions | No | List of strings (PostgreSQL extension names) | Allows you to represent PostgreSQL extensions in your schema. Available in preview for PostgreSQL only in Prisma ORM versions 4.5.0 and later. |
The following providers are available:
Remarks
- You can only have one
datasource
block in a schema. datasource db
is convention - however, you can give your data source any name - for example,datasource mysql
ordatasource data
.
Examples
Specify a PostgreSQL data source
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
5432
- Database name:
mydb
- Schema name:
public
datasource db {
provider = "postgresql"
url = "postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public"
}
Learn more about PostgreSQL connection strings here.
Specify a PostgreSQL data source via an environment variable
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
5432
- Database name:
mydb
- Schema name:
public
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
When running a Prisma CLI command that needs the database connection URL (e.g. prisma generate
), you need to make sure that the DATABASE_URL
environment variable is set.
One way to do so is by creating a .env
file with the following contents. Note that the file must be in the same directory as your schema.prisma
file to automatically picked up the Prisma CLI.
DATABASE_URL=postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public
Specify a MySQL data source
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
3306
- Database name:
mydb
datasource db {
provider = "mysql"
url = "mysql://johndoe:mypassword@localhost:3306/mydb"
}
Learn more about MySQL connection strings here.
Specify a MongoDB data source
- User:
root
- Password:
password
- Host:
cluster1.test1.mongodb.net
- Port: N/A
- Database name:
testing
datasource db {
provider = "mongodb"
url = "mongodb+srv://root:password@cluster1.test1.mongodb.net/testing?retryWrites=true&w=majority"
}
Learn more about MongoDB connection strings here.
Specify a SQLite data source
In this example, the target database is located in a file called dev.db
:
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
Learn more about SQLite connection strings here.
Specify a CockroachDB data source
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
26257
- Database name:
mydb
- Schema name:
public
datasource db {
provider = "cockroachdb"
url = "postgresql://johndoe:mypassword@localhost:26257/mydb?schema=public"
}
The format for connection strings is the same as for PostgreSQL. Learn more about PostgreSQL connection strings here.
generator
Defines a generator in the Prisma schema.
Fields
A generator
block accepts the following fields:
Name | Required | Type | Description |
---|---|---|---|
provider | Yes | String (file path) or Enum (prisma-client-js ) | Describes which generator to use. This can point to a file that implements a generator or specify a built-in generator directly. |
output | No | String (file path) | Determines the location for the generated client, learn more. Default: node_modules/.prisma/client |
previewFeatures | No | List of Enums | Use intellisense to see list of currently available Preview features (Ctrl+Space in Visual Studio Code) Default: none |
engineType | No | Enum (library or binary ) | Defines the query engine type to download and use. Default: library |
binaryTargets | No | List of Enums (see below) | Specify the OS on which the Prisma Client will run to ensure compatibility of the query engine. Default: native |
binaryTargets
options
The following tables list all supported operating systems with the name of platform to specify in binaryTargets
.
Unless specified otherwise, the default supported CPU architecture is x86_64.
macOS
Build OS | Prisma engine build name |
---|---|
macOS Intel x86_64 | darwin |
macOS ARM64 | darwin-arm64 |
Windows
Build OS | Prisma engine build name |
---|---|
Windows | windows |
Linux (Alpine on x86_64 architectures)
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Alpine (3.17 and newer) | linux-musl-openssl-3.0.x * | 3.0.x |
Alpine (3.16 and older) | linux-musl | 1.1.x |
* Available in Prisma ORM versions 4.8.0 and later.
Linux (Alpine on ARM64 architectures)
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Alpine (3.17 and newer) | linux-musl-arm64-openssl-3.0.x * | 3.0.x |
Alpine (3.16 and older) | linux-musl-arm64-openssl-1.1.x * | 1.1.x |
* Available in Prisma ORM versions 4.10.0 and later.