Back to Tutorials
Databases
40 min read
Sahasransu Satpathy
4/10/2026

MySQL & PostgreSQL Basics – Beginner Friendly Guide

Learn the fundamentals of relational databases, SQL syntax, and CRUD operations using MySQL and PostgreSQL

Introduction

Relational databases like MySQL and PostgreSQL are essential for storing and managing data in web applications. This tutorial covers SQL basics, database setup, and CRUD operations for beginners.


Step 1: Installing the Databases

MySQL

sudo apt install mysql-server
mysql --version

PostgreSQL

sudo apt install postgresql postgresql-contrib
psql --version

Step 2: Creating a Database

MySQL

CREATE DATABASE my_app;
USE my_app;

PostgreSQL

CREATE DATABASE my_app;
c my_app

Step 3: Creating Tables

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Step 4: Inserting Data

INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com');

Step 5: Querying Data

-- Select all users
SELECT * FROM users;

-- Select by condition
SELECT * FROM users WHERE name = 'Alice';

Step 6: Updating Data

UPDATE users
SET email = 'alice_new@example.com'
WHERE name = 'Alice';

Step 7: Deleting Data

DELETE FROM users
WHERE name = 'Bob';

Step 8: Using SQL in Node.js

const { Client } = require('pg'); // For PostgreSQL
const client = new Client({ connectionString: process.env.DATABASE_URL });

await client.connect();
const res = await client.query('SELECT * FROM users');
console.log(res.rows);
await client.end();

Step 9: Best Practices

  • Use parameterized queries to prevent SQL injection.
  • Normalize your data to reduce redundancy.
  • Backup your database regularly.
  • Use indexes for faster query performance.

Conclusion

You now know how to set up databases, create tables, and perform CRUD operations with MySQL and PostgreSQL. This foundation will help you build robust backend systems.


SEO Suggestions:

  • Main keywords: MySQL tutorial, PostgreSQL guide, SQL basics, database CRUD operations, beginner SQL tutorial
  • Meta description: Learn MySQL and PostgreSQL from scratch. Step-by-step guide covering database setup, SQL queries, and CRUD operations for beginners.
  • Catchy title suggestions: "MySQL & PostgreSQL Basics – Beginner-Friendly SQL Guide 2026", "Learn SQL: MySQL & PostgreSQL Step-by-Step Tutorial"

Previous Tutorial

Browse All Tutorials