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?
Many assume that databases float somewhere in the "cloud" conceptually, but the reality is physical:
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".
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.
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.
Instead of installing PostgreSQL directly on your system, developers use Docker to create lightweight, isolated environments (containers).
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" postgresFor 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" postgresFor 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
-vdo? This maps a new folder namedpgdatainside your current directory to the database's internal storage. Your data physically stays on your laptop!
If you need to restart or clean up, remove the container forcefully:
docker rm -f sql_classTo verify that your database is running smoothly, use:
docker psYou 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.
| 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 |
Think of hosting like a house setup:
"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.
"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
If a client asks you to build a system, here is how the real-world flow goes:
- 📝 Client Requirement: Understand what features are needed.
- 💻 Local Development: Write the code, run PostgreSQL via Docker, and test.
- 🚀 Deployment:
- Grab a free server using the GitHub Developer Pack.
- Push code to the server using Git.
- Run
docker compose up -don the server.
- 🎉 Client Delivery: Hand over the live URL (
myshop.com) to the client. Boom!
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.
[ 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 postgresIf 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 postgresIf 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 postgresdocker rm -f sql_class