Skip to main content
Reading progress: 0%

Lesson 9.1: Production Environments, Databases, and Deploying

Lesson Quick-Jump (10 Sections)

1. What Are We Learning?​

In this lesson, you will learn how to:

  • Understand the difference between local development and production environments.
  • Deploy your frontend and backend to standard hosting platforms.
  • Configure a production database safely.
  • Manage production environment variables without exposing secrets.

2. Why Does It Matter?​

info

An app that only works on http://localhost:3000 on your laptop helps nobody else. Moving to production introduces real-world differences: cloud servers restart, local file storage can vanish on stateless hosts, HTTPS is required, and production databases need real credentials configured in your host's dashboard—not a local .env file.

Understanding how deployment works prevents the classic "It worked on my machine, why did it crash online?" headache.


2.5 Note on Tools​

note

Hosting platforms change pricing and dashboards over time. This lesson teaches the universal concepts that apply to any standard provider (such as Render, Railway, Fly.io, Vercel, Netlify, or a VPS), so you can deploy anywhere without vendor lock-in.


3. Simple Explanation for a Complete Beginner​

  • Local Environment: Your personal laptop running the app privately (localhost). Great for testing fast; invisible to the outside world.
  • Production Environment: A real computer in a cloud data center running your app 24/7 at a public HTTPS URL (https://your-app.com) for real users.
  • Deploying: Connecting your GitHub main branch (or uploading built files) to a hosting provider so it installs dependencies, builds the app, starts the server, and routes traffic to it.
  • Production Database Warning (Very Important!):
    • On many modern cloud hosts, the server's temporary hard drive resets every time you redeploy! If you saved data in a plain local file or ephemeral SQLite file without persistent disk storage, all user data wipes on the next deploy.
    • For production apps with persistent user data, use either a managed database service (like managed PostgreSQL) or an explicitly configured persistent volume.
  • Production Environment Variables:
    • Since .env is excluded by .gitignore (and must NEVER be pushed to GitHub), you enter your production DATABASE_URL, SESSION_SECRET, and API keys directly in your hosting provider's secure Environment Variables / Secrets settings panel.

4. Practical Example​

Deploying a Bookmark Vault app (Frontend + Backend + Postgres Database):

  1. Database first: Provision a managed PostgreSQL database on your host -> copy the private DATABASE_URL connection string.
  2. Environment variables: In your app's hosting settings, configure:
    • DATABASE_URL = (the managed production Postgres URL)
    • SESSION_SECRET = (a brand-new long random secret—never reuse your local "dev-secret"!)
    • NODE_ENV / APP_ENV = production
  3. Build & Start commands:
    • Build command (example): npm install && npm run build
    • Migration step (creating tables in the fresh production database): npm run db:migrate
    • Start command: npm start
  4. Deploy from main: Trigger deployment, watch the build logs, and wait for the live HTTPS URL!

5. Step-by-Step Instructions​

  1. Run a local production-mode check first: Test building your app locally (e.g., npm run build or equivalent) to catch build errors before pushing to a server.
  2. Audit .env.example: Make sure every environment variable your code reads is listed in .env.example so you don't forget one during setup.
  3. Choose hosting that matches your stack:
    • Static/Frontend-only apps -> Static/frontend hosts.
    • Backend API / Full-stack server + Database -> Server host + managed database (or persistent storage).
  4. Provision your production database (if your app stores data on a server) and run database schema setup/migrations cleanly.
  5. Enter production environment variables in your hosting dashboard (generate fresh, strong random secrets for production—never copy "test123").
  6. Connect your GitHub main branch and deploy, watching the Build Logs and Runtime Logs carefully.
tip

Unfamiliar hosting dashboard? Watch one quick 5–20 minute YouTube deploy walkthrough for your chosen host to orient yourself, then never hesitate to screenshot confusing settings in Chrome (blurring/hiding secret values!) or copy-paste build logs into AI with Prompt 6: Break Down a Confusing Screen.


6. Ready-to-Use AI Prompt​

Context:
I am preparing to deploy my MVP to production for the first time.
- Tech stack: [describe your frontend, backend, database]
- Hosting approach I'm considering: [name provider or ask for 2 beginner-friendly options]
- `.env.example` contents:
[Paste your `.env.example` variable names—NEVER real values]

Objective:
Create a step-by-step Pre-Deployment & Deployment Checklist tailored to this exact codebase and stack.

Boundaries:
- Inspect the repo (`package.json` / `requirements.txt`, config files, database setup) before answering.
- Do NOT ask me to share real production secrets or passwords.
- Explicitly warn me if my database/file storage would lose data on redeploy.

Expected Output:
1. Codebase readiness check (hardcoded `localhost` URLs, missing `PORT` env handling, build scripts, or missing `.env.example` keys).
2. Exact Build command, Start command, and Database migration step needed.
3. Step-by-step list of environment variables to configure on the hosting dashboard.
4. Common deployment failure points for this stack and how to spot them in logs.

7. What to Expect from the AI Agent​

Good ResponseWarning Sign
The agent checks your config files, spots hardcoded http://localhost:3000 URLs that would break online, checks whether your server listens on the host-provided PORT environment variable, and gives clear database persistence advice.The agent tells you to commit your .env file temporarily "just to make deployment easier," or skips explaining how your database schema gets created in production. Never commit .env to Git!

8. What You Must Personally Verify​

  • No hardcoded localhost links: Ensure API URLs and redirect/CORS settings read from environment variables or relative paths, not hardcoded localhost.
  • Database persistence confirmed: Verify your production database is separate from temporary server disk (or uses an attached persistent volume) so redeploying does not erase user data.
  • Production secrets are unique: Generate strong, fresh secrets for production—never reuse local dev passwords.
  • HTTPS works: Confirm your deployed site opens on https:// without browser security warnings.

9. Common Mistakes​

  1. Forgetting to create database tables in production: Your app boots cleanly, then crashes on the first click with relation "habits" does not exist because migrations ran locally weeks ago but never ran on the production database.
  2. Missing environment variable in the host dashboard: Everything works locally because .env existed on your laptop, but production crashes immediately because DATABASE_URL wasn't added in the hosting settings.
  3. Using a dev database locally and accidentally connecting local tests to production: Keep local and production databases strictly separated!

10. Small Practical Exercise​

  1. Run the Pre-Deployment AI Prompt on your project repository.
  2. Check your codebase for any hardcoded localhost strings, missing PORT binding, or undocumented environment variables.
  3. Deploy your MVP (or practice deploying a simple starter version on a free beginner tier) and confirm the build + startup logs succeed cleanly.

Completed the exercise? Track your progress

Check off this lesson in PROGRESS-CHECKLIST.md once you have verified every step in Section 8.