Repaso
Español
Cloud & DevOpsCoreOPS-35

Why read DATABASE_URL from an environment variable instead of writing it in the code?

Options

  1. It is faster to read a value from the environment than from code
  2. The same code runs in each environment with different values (Correct option)
  3. Python requires that all the settings come from the environment
  4. It encrypts the URL while the program is running in memory

Answer

The same code can run in dev, staging and prod with different values, and secrets stay out of the code

Why

Environment variables are values provided to a program by the system that runs it. In Python: os.environ["DATABASE_URL"].

Your laptop, the staging server and production each set their own value, while the code stays identical.

Passwords inside the URL never get committed to git.

Remember

Settings and secrets → environment variables.

Key terms

Practice this question in Repaso