Skip to content

abdulrdeveloper/Learn-SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 

Repository files navigation

🚀 The Ultimate Backend Guide

Servers, Databases, and Deployments!


This guide is designed for beginners and self-learners who want to understand the foundational concepts: "What actually happens under the hood?" Where is data stored? What is a server? How does Docker work? And how are applications deployed to production?



🖥️ 1. What are Servers and Databases?

Many assume that databases float somewhere in the "cloud" conceptually, but the reality is physical:

🔹 What is a Server?

A server is simply a computer that runs 24/7. However, it's not your regular home PC. It is a highly powerful machine kept in a data center (like Amazon AWS, Google GCP, Microsoft Azure, or DigitalOcean), featuring backup power and enterprise cooling.

When you type google.com, your browser sends a request to their server. Inside the server, software written in Node.js, Python (Django), or PHP processes your request. This is the "Backend".

🔹 Where does the Database live?

Whenever a database saves data, it ultimately writes that data to files on the server's SSD or Hard Drive:

Database Storage Format
MySQL/PostgreSQL .ibd or Write-Ahead Log (WAL) files
MongoDB .bson format files

These files sit on the storage drive just like your personal files on your laptop.

🔹 What does "Connecting" mean?

When you write code like:

mongoose.connect("mongodb://localhost:27017/mydb")
// OR
const pool = new Pool({ host: 'localhost', port: 5432 })

💡 Core Concept: You are basically telling your backend: Listen at this port (doorway), the database daemon is running there. It establishes a permanent TCP connection line.



🐳 2. Docker Setup: Running a Database Locally

Instead of installing PostgreSQL directly on your system, developers use Docker to create lightweight, isolated environments (containers).

🛠️ Create a Persistent Database Container

If you want to run a container and save all its database files physically inside your current working folder (so you never lose data even if the container is deleted), run this in your terminal:

For Mac / Ubuntu / Linux / WSL
docker run -d --name sql_class -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=sql_class_01_db -p 5433:5432 -v "$(pwd)/pgdata:/var/lib/postgresql/data" postgres
For Windows (PowerShell) *Make sure to use `${PWD}`*
docker run -d --name sql_class -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=sql_class_01_db -p 5433:5432 -v "${PWD}/pgdata:/var/lib/postgresql/data" postgres
For Windows (Command Prompt) *Make sure to use `%cd%`*
docker run -d --name sql_class -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=sql_class_01_db -p 5433:5432 -v "%cd%/pgdata:/var/lib/postgresql/data" postgres

📌 What does -v do? This maps a new folder named pgdata inside your current directory to the database's internal storage. Your data physically stays on your laptop!

🧹 Removing an Existing Container

If you need to restart or clean up, remove the container forcefully:

docker rm -f sql_class

🔍 Check Running Containers

To verify that your database is running smoothly, use:

docker ps

You will see your container listed along with its mapped ports. You can also view and manage this container easily through the Docker Desktop GUI dashboard.



💻 3. Local Setup vs. Production VPS

Environment Description Best For
🏠 Your Laptop (Local Server) When you run Docker commands locally, your laptop is the server. Data goes offline when turned off. Development and testing
🌐 Production (VPS) Rent a Virtual Private Server (e.g., ~$5/mo DigitalOcean Droplet). It stays live 24/7. Connect via ssh. Live internet deployment


🏗️ 4. Hosting Architectures: VPS vs Managed DB

Think of hosting like a house setup:

🏢 Method 1: All-in-One (The VPS Way)

"A big house where you keep everything."

  • Setup: You rent one server. You put both your Node.js code and your DB container on this exact same machine.
  • URL: postgresql://user:pass@localhost:5432/mydb
  • Pros: Cheap, gives full control, perfect for learning.

🏙️ Method 2: Split System (e.g., Vercel + Supabase)

"Keeping your living room in one city and your store-room in another."

  • Code: Hosted on Vercel or Railway.
  • Database: Hosted on Supabase (Managed DB provider).
  • URL: postgresql://user:[email protected]:5432/mydb


🌍 5. The Complete Developer Journey

If a client asks you to build a system, here is how the real-world flow goes:

  1. 📝 Client Requirement: Understand what features are needed.
  2. 💻 Local Development: Write the code, run PostgreSQL via Docker, and test.
  3. 🚀 Deployment:
    • Grab a free server using the GitHub Developer Pack.
    • Push code to the server using Git.
    • Run docker compose up -d on the server.
  4. 🎉 Client Delivery: Hand over the live URL (myshop.com) to the client. Boom!


🤔 6. Bonus: Why Learn Raw SQL When We Have ORMs?

A common beginner question is: "If Drizzle or Prisma allow me to write JS/TS code that automatically executes DB queries, why waste time mastering raw SQL?"

Here is why true professionals always know SQL perfectly:

  • 🔍 Under the Hood, It’s Still SQL! ORMs translate JS into SQL strings. If you don't know SQL, you don't know what instructions Prisma is generating.
  • 🚀 The Dreaded N+1 Problem: ORMs can generate terrible queries (e.g., sending 100 queries instead of 1 optimized query). Knowing SQL helps spot bottlenecks.
  • 🤯 Complex Logic Breaks ORMs: For complex financial reports or JOINing 6 tables, ORM syntax becomes a headache. Developers fallback to Raw SQL queries.
  • 🐛 Debugging Server Crashes: Production logs show raw SQL operations executing EXPLAIN ANALYZE, not TypeScript.
  • SQL is Immortal: ORM trends shift constantly (Sequelize 👉 Prisma 👉 Drizzle). SQL has remained exactly the same for 50 years!

🎯 Final Takeaway: Read the documentation, understand the foundations. Before you rely on an abstraction (shortcut), know exactly how the machine operates underneath.



⚡ Quick Reference Commands

🛠️ For Creating Your First Database Container (with persistent data)

[ works in MAC / Ubuntu / Linux / WSL ]

docker run -d --name sql_class -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=sql_class_01_db -p 5433:5432 postgres

🪟 For Windows

If you are on PowerShell then use ${PWD}:

docker run -d --name sql_class -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=sql_class_01_db -p 5433:5432 -v ${PWD}/pgdata:/var/lib/postgresql/data postgres

If you are on Command Prompt use %cd%:

docker run -d --name sql_class -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=sql_class_01_db -p 5433:5432 -v %cd%/pgdata:/var/lib/postgresql/data postgres

🧹 For Removing existing container, run this:

docker rm -f sql_class

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors