liveBook · Manning

Environment variable

As explained in Build Financial Software with Generative AI (From Scratch)

An environment variable is a named value supplied through a process environment to configure software without embedding the value in source code.

An environment variable is a named value supplied through a process environment, letting software use configuration such as usernames, passwords, ports, or servers without embedding those values in source code. This reduces security exposure and prevents maintenance problems when environments change. section 3.7

Use environment variables to:

  • Pass PostgreSQL credentials to a Docker service through an environment file. section 3.7
  • Read configuration inside application code with Python's os.getenv function. section 3.7
  • Keep sensitive configuration out of source control while documenting setup for new instances and team members. section 3.7

Dockerized PostgreSQL configuration

A configuration file can define POSTGRES_USER=postgres and POSTGRES_PASSWORD=secret. Docker Compose supplies that file with env_file, while the API retrieves the values with os.getenv and builds its database connection without hardcoding credentials. section 3.7

  postgres:
    build: 
      context: ./db
      dockerfile: Dockerfile
    env_file:
      - ./.sql_server.conf

Shows how Docker attaches an environment file to a PostgreSQL service. section 3.7

import os
…
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
…
DATABASE_URL = f"dbname={POSTGRES_USER} user={POSTGRES_USER}
➥ password={POSTGRES_PASSWORD} host=postgres port=5432"

Shows application code reading PostgreSQL settings from the process environment. section 3.7

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Demonstrates environment files, Docker wiring, Python retrieval, source-control protection, and documentation, but not deeper process-environment internals.
Examples
Uses PostgreSQL credentials, a Docker env_file directive, and Python os.getenv database configuration.
What it assumes
Assumes a Dockerized application with a PostgreSQL service and introduces configuration externalization through environment files.
What you can do after
Operate/configure Docker services and application database connections while avoiding hardcoded sensitive values.
How it connects
Connects Docker, PostgreSQL, Docker Compose, Python, source control, security, and configuration management.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.