-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into prisma-migrate-hybrid
- Loading branch information
Showing
4 changed files
with
122 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...-guides/125-development-environment/050-environment-variables/040-env-files.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
title: '.env files' | ||
metaTitle: '.env files' | ||
metaDescription: 'Configure environment variables using .env files in Prisma' | ||
--- | ||
|
||
<TopBlock> | ||
|
||
Prisma creates a default `.env` file at your projects root. You can choose to replace this file or create a new one in the `prisma` folder, or if you choose to relocate your `prisma.schema` file, alongside that. | ||
|
||
</TopBlock> | ||
|
||
### <inlinecode>.env</inlinecode> file locations | ||
|
||
The Prisma CLI looks for `.env` files, in order, in the following locations: | ||
|
||
1. In the root folder of your project (`./.env`) | ||
1. From the same folder as the schema specified by the `--schema` argument | ||
1. From the same folder as the schema taken from `"prisma": {"schema": "/path/to/schema.prisma"}` in `package.json` | ||
1. From the `./prisma` folder | ||
|
||
If a `.env` file is located in step 1., but additional, clashing `.env` variables are located in steps 2. - 4., the CLI will throw an error. For example, if you specify a `DATABASE_URL` variable in two different `.env` files, you will get the following error: | ||
|
||
``` | ||
Error: There is a conflict between env vars in .env and prisma/.env | ||
Conflicting env vars: | ||
DATABASE_URL | ||
We suggest to move the contents of prisma/.env to .env to consolidate your env vars. | ||
``` | ||
|
||
The following table describes where the Prisma CLI looks for the `.env` file: | ||
|
||
| **Command** | **Schema file location** | **`.env` file locations checked, in order** | | ||
| :---------------------------------------------- | :--------------------------------------------------------------------------- | :-------------------------------------------------------- | | ||
| `prisma [command]` | `./prisma/schema.prisma` | `./.env`<br />`./prisma/.env` | | ||
| `prisma [command] --schema=./a/b/schema.prisma` | `./a/b/schema.prisma` | `./.env`<br />`./a/b/.env`<br />`./prisma/.env` | | ||
| `prisma [command]` | `"prisma": {"schema": "/path/to/schema.prisma"}` | `.env`<br /> `./path/to/schema/.env`<br />`./prisma/.env` | | ||
| `prisma [command]` | No schema (for example, when running `prisma db pull` in an empty directory) | `./.env`<br />`./prisma/.env` | | ||
|
||
Any environment variables defined in that `.env` file will automatically be loaded when running a Prisma CLI command. | ||
|
||
<Admonition type="warning"> | ||
|
||
**Do not commit your `.env` files into version control**! | ||
|
||
</Admonition> | ||
|
||
Refer to the `dotenv` documentation for information about [what happens if an environment variable is defined in two places](https://www.npmjs.com/package/dotenv#what-happens-to-environment-variables-that-were-already-set). | ||
|
||
### Expanding variables | ||
|
||
Variables stored in `.env` files can be expanded using the format specified by [dotenv-expand](https://github.com/motdotla/dotenv-expand). | ||
|
||
```env file=.env | ||
DATABASE_URL=postgresql://test:test@localhost:5432/test | ||
DATABASE_URL_WITH_SCHEMA=${DATABASE_URL}?schema=public | ||
``` | ||
|
||
This will make the environment variable `DATABASE_URL_WITH_SCHEMA` with value `postgresql://test:test@localhost:5432/test?schema=public` available for Prisma. | ||
|
||
You can also use environment variables in the expansion that are set _outside_ of the `.env` file, for example a database URL that is set on a PaaS like Heroku or similar: | ||
|
||
```terminal | ||
# environment variable already set in the environment of the system | ||
export DATABASE_URL=postgresql://test:test@localhost:5432/test | ||
``` | ||
|
||
```env file=.env | ||
DATABASE_URL_WITH_SCHEMA=${DATABASE_URL}?schema=foo | ||
``` | ||
|
||
This will make the environment variable `DATABASE_URL_WITH_SCHEMA` with value `postgresql://test:test@localhost:5432/test?schema=foo` available for Prisma. | ||
|
||
### Example: Set the `DATABASE_URL` environment variable in an `.env` file | ||
|
||
It is common to load your database connection URL from an environment variable: | ||
|
||
```prisma | ||
// schema.prisma | ||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
``` | ||
|
||
You can set the `DATABASE_URL` in your `.env` file: | ||
|
||
```env file=.env | ||
DATABASE_URL=postgresql://test:test@localhost:5432/test?schema=public | ||
``` | ||
|
||
When you run a command that needs access to the database defined via the `datasource` block (for example, `prisma db pull`), the Prisma CLI automatically loads the `DATABASE_URL` environment variables from the `.env` file and makes it available to the CLI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters