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


















