Example Walkthrough: Moving a Connection String From Laptop to Hosting
This illustrative walkthrough follows an invented learner, Learner C, as they move a small app from their laptop to a hosting platform. The details are generic and made up; check your own provider's documentation for exact steps.
Where things start
On the laptop, Learner C's app connects to a PostgreSQL server running locally:
postgresql://dev:devpass@localhost:5432/notes
The string sits directly in the code, which is convenient while learning.
Step 1: notice what will change
On the hosting platform, the database runs somewhere else. Learner C compares the two situations:
| Part | Laptop | Hosting |
|---|---|---|
| Host | localhost | a host name from the provider |
| Port | 5432 | often 5432, but check |
| User and password | dev / devpass | new, stronger credentials |
| Database name | notes | whatever was created on the host |
| Encryption | not needed locally | usually required |
The *shape* of the string stays the same. The values change.
Step 2: get the string out of the code
Before uploading anything, Learner C moves the connection string into an environment variable, a setting the operating system or platform passes to the program:
import os
db_url = os.environ["DATABASE_URL"]
On the laptop, they set DATABASE_URL in a local .env file and add that file to .gitignore, so it is never committed. On the hosting platform, they paste the real value into the platform's settings page for environment variables.
Step 3: add encryption
The provider's documentation says connections must use TLS. Learner C adds the option to the hosted string, for example ?sslmode=require for PostgreSQL. The first attempt without it fails with an error mentioning SSL, which is a useful clue to remember.
Step 4: create a separate database user
Rather than using the main admin account, Learner C creates a user just for the app with permission only on the notes database. If the app's credentials ever leak, the damage is limited.
Step 5: test the connection on its own
Before deploying the whole app, Learner C runs a tiny script on the host that connects and runs SELECT 1. It works. Then the full app goes live.
Decision points
| Question | Choice in this example | Reason |
|---|---|---|
| Keep the string in code? | No | Code gets shared; secrets should not |
| Use admin account? | No | Limit damage if credentials leak |
| Skip TLS? | No | Hosted connections cross networks |
| Test separately first? | Yes | Easier to see connection errors on their own |
What went wrong along the way (and is common)
- The password contained an
@, which confused the URL. Encoding it, or choosing a password without that symbol, fixed it. - The app on the host started before the variable was set and crashed. Setting the variable and restarting solved it.
Takeaway
Moving to hosting is mostly about the values in the connection string and where you keep them. Our connection string generator and guide to keeping a connection string in an environment variable cover each step in more detail.
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 example: the organisations, people, projects and results described on this page are hypothetical. They do not describe a real client or engagement, and no consulting services are offered here.