PostgreSQL (often just βPostgresβ) is a relational database. It stores data in tables with rows and columns, and you query it with SQL.
Itβs the most popular open-source database for production applications, and for good reason β itβs reliable, feature-rich, and handles everything from a side project to millions of users.
How it works
You define tables, insert data, and query it with SQL:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
SELECT * FROM users WHERE email = 'alice@example.com';
Tables can reference each other (thatβs the βrelationalβ part):
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
user_id INTEGER REFERENCES users(id)
);
-- Get all posts with their author name
SELECT posts.title, users.name
FROM posts
JOIN users ON posts.user_id = users.id;
Why developers choose PostgreSQL
- ACID compliant β your data is safe, even during crashes
- JSON support β store and query JSON alongside relational data
- Full-text search β built-in search without Elasticsearch
- Extensions β PostGIS for geospatial, pgvector for AI embeddings
- Scales well β handles millions of rows without breaking a sweat
- Free and open source β no licensing costs, ever
PostgreSQL vs. alternatives
| Database | Type | Best for |
|---|---|---|
| PostgreSQL | Relational (SQL) | Most applications, complex queries |
| MySQL | Relational (SQL) | WordPress, simpler use cases |
| SQLite | Relational (SQL) | Embedded, local-first, small apps |
| MongoDB | Document (NoSQL) | Flexible schemas, rapid prototyping |
| Redis | Key-value (in-memory) | Caching, sessions, queues |
Where to host PostgreSQL
- Supabase β free tier, managed Postgres with auth and APIs
- Neon β serverless Postgres, generous free tier
- Railway β simple managed Postgres
- AWS RDS β production-grade, more setup
- Local β
brew install postgresqlor Docker
Using PostgreSQL in your app
Most developers use an ORM or query builder instead of raw SQL:
- Prisma (Node.js/TypeScript) β type-safe ORM
- Drizzle (Node.js/TypeScript) β lightweight, SQL-like
- SQLAlchemy (Python) β full-featured ORM
- Django ORM (Python) β built into Django
For the full command reference, see the PostgreSQL cheat sheet and SQL cheat sheet.