Managing .env files and setting variables

Prisma ORM creates an .env file for you upon project initialization with prisma init. You are not limited to using that file, some other options include:

  • Do not use .env files and let Prisma ORM use the system environment variables directly
  • Use .env files from a location that the Prisma CLI does not check by default
  • Use multiple .env file

Using the system environment directly

Because Prisma ORM reads from the system's environment when looking for environment variables, it's possible to skip using .env completely and create them manually on your local system.

The following examples will use setting the DATABASE_URL environment variable which is often used for the database connection URL.

Manually set an environment variable on a Mac/Linux system

From a terminal on a Unix machine (Mac/Linux), you export the variable as a key value pair.

$export DATABASE_URL=postgresql://test:test@localhost:5432/test?schema=public

Then check that it has been successfully set using printenv:

$printenv DATABASE_URL
Hide CLI results
postgresql://test:test@localhost:5432/test?schema=public

Manually set an environment variable on a Windows system

The following examples illustrate how to set the environment variable (for the current user) using both Command Prompt (cmd.exe) and PowerShell, depending on your preference.

Command Prompt
Powershell
$set DATABASE_URL="postgresql://test:test@localhost:5432/test?schema=public"

Then check that it has been successfully set:

Command Prompt
Powershell
$set DATABASE_URL

Manage .env files manually

The and packages can be used if you want to manage your .envfiles manually.

They allow you to:

Using dotenv-cli via command line

The following steps show how to use the dotenv-cli package to use an alternative file to contain environment variables than the default created by Prisma ORM, which is then used to run Introspection.

  1. Install :

    npm install -g dotenv-cli
  2. Create a file - for example, .env3 - in your project's root folder.

  3. To use the .env3 file, you can use dotenv when you run any Prisma ORM command and specify which .env file to use. The following example uses a file named .env3:

    dotenv -e .env3 -- npx prisma db pull

Note: dotenv doesn't pass the flags to the Prisma ORM command by default, this is why the command includes two dashes -- before prisma, making it possible to use flags like --force, --schema or --preview-feature.

Using dotenv via application code

The following steps show how to use the dotenv package to reference an alternative environment file in your project's code.

  1. Add to your project:

    npm install dotenv
  2. Create a file - for example, .env3 - in your project's root folder.

  3. To use the .env3 file, include a reference to dotenv at the top of your project's entry file.

    import { config } from 'dotenv'
    config({ path: '.env3' })