Connection Strings: What Is Different Now
A connection string tells your app where the database is and how to log in. The basic idea is decades old, but where people keep these strings and what goes in them has changed. Here is what is different now, explained for beginners.
Then: details written into the code
Older examples often put the host, user name and password straight into the source code:
# old habit: avoid this
conn = psycopg.connect("host=db.example.com user=admin password=secret123 dbname=shop")
The problem is that code gets copied, shared and uploaded. Passwords in code end up on public code-hosting sites more often than anyone would like.
Change 1: URL-style connection strings
Many tools now accept a single URL that holds all the parts:
postgresql://app_user:PASSWORD@localhost:5432/shop
Reading it left to right: the type of database, the user, the password, the host, the port and the database name. Many hosting platforms and frameworks use one variable, often called DATABASE_URL, containing a string like this.
Change 2: environment variables
"The Twelve-Factor App", a set of guidelines published by developers at Heroku, recommends keeping configuration such as database addresses in environment variables rather than in code. That idea is now widespread.
import os, psycopg
conn = psycopg.connect(os.environ["DATABASE_URL"])
The code stays the same on your laptop and on a server; only the environment changes. For local work, many people keep variables in a .env file that is listed in .gitignore so it is never committed.
Change 3: secrets managers
Larger projects store passwords in a secrets manager, a service whose job is to keep secrets safe, control who can read them and change them regularly. Cloud providers and open-source tools both offer these. The app fetches the secret when it starts.
Change 4: encrypted connections by default
It is now normal to encrypt the connection between app and database, especially over the internet. Connection strings often include an option such as sslmode=require (PostgreSQL) to ask for encryption. Hosted databases frequently insist on it.
Change 5: signing in without a stored password
Some cloud platforms let an app prove who it is using the platform's own identity system, issuing short-lived credentials instead of a long-lived password. The idea is that there is no permanent password to leak.
Then and now
| Then | Now |
|---|---|
| Password in source code | Password in environment variables or a secrets manager |
| Separate settings for host, port, user | Often one URL-style string |
| Unencrypted connections common | Encryption expected, especially over networks |
| Same password for years | Rotated regularly, or replaced by short-lived credentials |
What a beginner should do today
- Put your connection string in an environment variable or a
.envfile that git ignores. - Never commit passwords, even in a "private" repository.
- Use a different, limited user for your app, not the database administrator account.
Our how-to on keeping a connection string in an environment variable walks you through step 1.
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.