Notes From a Beginner Talk on Keeping Connection Strings Secret
Written session notes for a short, friendly talk. There is no real event or recording. The subject: your connection string usually contains a password, and where you keep it matters as much as what it says.
Agenda
- Why connection strings are secrets
- The beginner mistake
- Environment variables
- .env files and .gitignore
- If a password leaks
- Questions
1. Why they are secrets
A connection string like postgresql://shop_app:Hunter2x@db.example.com/shop contains everything needed to log in to your database. Anyone who sees it can read, and possibly change, your data. Treat it like a house key.
2. The beginner mistake
Writing it straight into code:
db = connect("postgresql://shop_app:Hunter2x@db.example.com/shop") # do not do this
The moment that file is shared, uploaded to a public code-hosting site or copied into a forum post, the password goes with it. Old versions in version control history keep it even after you delete the line.
3. Environment variables
An environment variable is a named value that the operating system gives to your program when it starts. Your code reads the name, not the secret:
import os
db = connect(os.environ["DATABASE_URL"])
On your laptop you set DATABASE_URL in your terminal or a local file. On a hosting platform you set it in the platform's settings page. The code is identical everywhere and contains no password.
4. .env files and .gitignore
Many projects keep local variables in a file named .env:
DATABASE_URL=postgresql://shop_app:localpass@localhost:5432/shop_dev
Libraries such as python-dotenv (Python) and dotenv (Node.js) load it at startup. Two rules:
- Add
.envto your.gitignorefile so it is never committed. - Commit a
.env.examplefile instead, with the variable names and fake values, so others know what to set.
5. If a password leaks
- Change the database user's password straight away.
- Update the environment variable wherever the app runs.
- Check the database's logs or activity for anything unusual, if you can.
- Remove the secret from the code, and remember that history may still hold it.
Good habits, in short
| Habit | Why |
|---|---|
| One database user per app | You can change or limit one app without affecting others |
| Minimal permissions | A leaked password can do less damage |
| Different passwords for development and production | A leak from a laptop does not open production |
| Encrypted connections | Passwords are not readable on the network |
Questions
"Is a private repository safe enough?" Safer, but repositories get shared, copied and made public by accident. Keep secrets out anyway.
"Can I just encode the password, for example in base64?" Encoding is not encryption; anyone can decode it. Use environment variables or your platform's secret storage.
"What about SQLite?" SQLite has no password, but the file itself is your data. Keep it out of public folders and repositories if it holds anything private.
Our step-by-step how-to on keeping a connection string in an environment variable puts this into practice.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.