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
.envfiles and let Prisma ORM use the system environment variables directly - Use
.envfiles from a location that the Prisma CLI does not check by default - Use multiple
.envfile
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
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"
[Environment]::SetEnvironmentVariable("DATABASE_URL", "postgresql://test:test@localhost:5432/test?schema=public")
Then check that it has been successfully set:
- Command Prompt
- Powershell
set DATABASE_URL
Get-ChildItem Env:DATABASE_URL
Manage .env files manually
The dotenv-cli and dotenv packages can be used if you want to manage your .envfiles manually.
They allow you to:
- Use multiple
.envfiles - Use
.envfiles from a location that the Prisma CLI does not check by default
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.
-
Install
dotenv-cli:npm install -g dotenv-cli -
Create a file - for example,
.env3- in your project's root folder. -
To use the
.env3file, you can usedotenvwhen you run any Prisma ORM command and specify which.envfile 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
--beforeprisma, making it possible to use flags like--force,--schemaor--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.
-
Add
dotenvto your project:npm install dotenv -
Create a file - for example,
.env3- in your project's root folder. -
To use the
.env3file, include a reference todotenvat the top of your project's entry file.import { config } from 'dotenv'
config({ path: '.env3' })