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.
.envfiles 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."
More on Database connections in apps
How does an application connect to a database, and how should connection details be passed?BeginnerAfter deployment, the app can't connect to the database. How do you troubleshoot it?IntermediateWhat is a connection string? Explain its parts.BeginnerWhat are the default ports for common databases?BeginnerHow do Kubernetes Secrets work, and are they secure?IntermediateWhat is the External Secrets Operator?Intermediate