$ commitlog.xyz
commit e6f7a8b
Author: You Date: Tue, Feb 10, 2026 9 min read

Getting Started with PostgreSQL

Introduction to PostgreSQL

PostgreSQL is a powerful, open-source relational database with excellent performance and features.

Installation

# macOS
brew install postgresql@16
brew services start postgresql@16

# Ubuntu
sudo apt install postgresql
sudo systemctl start postgresql

Basic Queries

-- Create a table
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) UNIQUE NOT NULL,
  name VARCHAR(100),
  created_at TIMESTAMP DEFAULT NOW()
);

-- Insert data
INSERT INTO users (email, name)
VALUES ('user@example.com', 'John Doe');

-- Query with conditions
SELECT * FROM users
WHERE created_at > NOW() - INTERVAL '7 days';

Indexes for Performance

-- Create an index
CREATE INDEX idx_users_email ON users(email);

-- Analyze query performance
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'user@example.com';

Transactions

BEGIN;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT;
-- or ROLLBACK on error

JSON Support

-- Store JSON data
CREATE TABLE events (
  id SERIAL PRIMARY KEY,
  data JSONB NOT NULL
);

-- Query JSON
SELECT * FROM events
WHERE data->>'type' = 'click';

Conclusion

PostgreSQL is an excellent choice for applications requiring reliability and advanced features.