Managing .env files and setting variables
Prisma creates an .env
file for you upon installation. You are not limited to using that file, some other options include:
- Do not use
.env
files and let Prisma 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 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.
$set DATABASE_URL="postgresql://test:test@localhost:5432/test?schema=public"
Then check that it has been successfully set:
$set DATABASE_URL
Manage .env files manually
The dotenv-cli
and dotenv
packages can be used if you want to manage your .env
files manually.
They allow you to:
- Use multiple
.env
files - Use
.env
files 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, which is then used to run Introspection.
Install
dotenv-cli
:npm install -g dotenv-cliCreate a file - for example,
.env3
- in your project's root folder.To use the
.env3
file, you can usedotenv
when you run any Prisma 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 command by default, this is why the command includes two dashes
--
beforeprisma
, 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.
Add
dotenv
to your project:npm install dotenvCreate a file - for example,
.env3
- in your project's root folder.To use the
.env3
file, include a reference todotenv
at the top of your project's entry file.import { config } from 'dotenv'config({ path: '.env3' })