Why should database credentials never be stored in code or Git?

BeginnerConceptDatabase connections in apps
Answer

Short answer

Because anyone with access to the repository — today or in the future — can read them, and Git keeps history forever: deleting the password in a later commit doesn't remove it. Leaked repos, forks, CI logs and laptops become a direct path to your production data. Credentials belong in a secret manager, injected at runtime.

What actually happens after a leak

  • Bots scan public GitHub for secrets within minutes of a push.
  • Internal repos leak too: ex-employees, compromised laptops, overly broad access.
  • The only real fix after a leak is to rotate the password — cleaning history is secondary.

The right pattern

Secret store (Vault / AWS Secrets Manager / Azure Key Vault)
   → injected into the runtime (K8s Secret via External Secrets, ECS secrets, App Service Key Vault refs)
   → app reads an environment variable or mounted file

Guardrails

  • Gitleaks or GitHub secret scanning in pre-commit and CI.
  • .env files in .gitignore.
  • Separate credentials per environment; least-privilege DB users.

What to say in the interview

"Code and Git history are widely accessible and permanent, so credentials there are effectively public. We keep them in a secret manager, inject them at runtime, scan commits with Gitleaks, and rotate immediately if anything leaks."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Connecting apps to databases

More on Database connections in apps