Overview

A data source can be specified using a datasource block in the Prisma schema file.

Fields

A datasource block accepts the following fields:

NameRequiredTypeDescription
providerYesEnum (postgresql, mysql, sqlite)Describes which data source connector to use.
urlYesString (URL)Connection URL including authentication info. Most connectors use the syntax provided by the database, learn more.

Examples

Specify a PostgreSQL data source

In this example, the target database is available with the following credentials:

  • User: johndoe
  • Password: mypassword
  • Host: localhost
  • Post: 5432
  • Database name: mydb
  • Schema name: public
1datasource postgresql {
2 provider = "postgresql"
3 url = "postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public"
4}

Learn more about PostgreSQL connection strings here.

Specify a MySQL data source

In this example, the target database is available with the following credentials:

  • User: johndoe
  • Password: mypassword
  • Host: localhost
  • Post: 5432
  • Database name: mydb
1datasource mysql {
2 provider = "mysql"
3 url = "mysql://johndoe:mypassword@localhost:5432/mydb"
4}

Learn more about MySQL connection strings here.

Specify a SQLite data source

In this example, the target database is located in a file called dev.db:

1datasource sqlite {
2 provider = "sqlite"
3 url = "file:./dev.db"
4}

Learn more about SQLite 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
  • Post: 5432
  • Database name: mydb
  • Schema name: public
1datasource postgresql {
2 provider = "postgresql"
3 url = env("DATABASE_URL")
4}

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.

1DATABASE_URL=postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public
Edit this page on Github