PostgreSQL
Overview
PostgreSQL is a mature, open-source object-relational database emphasizing standards compliance, ACID transactions, and extensibility. Read this when choosing a relational store, designing schemas, writing SQL, or tuning indexes and queries for a Postgres backend.
Official sources
Install / setup
Ubuntu, via the PostgreSQL Apt repository (from postgresql.org/download/linux/ubuntu/):
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt update
sudo apt install postgresql-18
The simplest path (distribution-included package) is:
apt install postgresql
Core concepts
- Relational model with ACID transactions; everything runs inside a transaction (explicit or implicit).
- Rich type system: beyond standard SQL types, native JSON/JSONB, arrays, ranges, and user-defined types.
- MVCC (Multi-Version Concurrency Control): readers don't block writers and vice versa; old row versions are cleaned by VACUUM.
- Indexes: B-tree (default), plus GIN, GiST, BRIN, and Hash for specialized access patterns (e.g., GIN for JSONB/full-text).
- Extensions: functionality like PostGIS or pg_trgm loaded via
CREATE EXTENSION.
- Roles and privileges: a unified role system handles both users and groups.
- Schemas: namespaces within a database for organizing objects.
Best practices
- Use the appropriate index type for the query; verify plans with
EXPLAIN ANALYZE before adding indexes blindly.
- Prefer
JSONB over JSON when you need to index or query inside documents (binary form supports GIN indexing).
- Let autovacuum run, and monitor for table bloat on high-churn tables (see docs on routine vacuuming).
- Use connection pooling for high-concurrency apps; each connection is a backend process.
- Define explicit constraints (NOT NULL, FOREIGN KEY, CHECK, UNIQUE) so the database enforces integrity.
Common pitfalls
- Skipping
EXPLAIN ANALYZE and assuming an index is used → confirm the planner actually chooses it.
- Treating
JSON and JSONB as interchangeable → JSON preserves text and is not efficiently indexable; use JSONB for querying.
- Ignoring VACUUM/autovacuum on write-heavy tables → leads to bloat and transaction ID wraparound risk.
- Opening one DB connection per request without pooling → process exhaustion under load.
Examples
CREATE TABLE users (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
profile JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_users_profile ON users USING GIN (profile);
Further reading
Related skills
- ../mysql — alternative open-source RDBMS
- ../supabase — hosted Postgres with auth/storage/realtime
- ../neon — serverless Postgres
- ../cockroachdb — distributed, Postgres-wire-compatible SQL
1---2name: postgresql3description: Robust open-source relational database with strong SQL, ACID, and extensibility; consult for schema design, transactions, and indexing.4domain: stack5category: database6tags: [postgresql, sql, rdbms, relational, acid]7official_sources:8 - https://www.postgresql.org/docs/9 - https://github.com/postgres/postgres10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/postgresql13---1415# PostgreSQL1617## Overview18PostgreSQL is a mature, open-source object-relational database emphasizing standards compliance, ACID transactions, and extensibility. Read this when choosing a relational store, designing schemas, writing SQL, or tuning indexes and queries for a Postgres backend.1920## Official sources21- Docs: https://www.postgresql.org/docs/22- Repo (official GitHub mirror; development uses the project's own git): https://github.com/postgres/postgres23- Install / download: https://www.postgresql.org/download/2425## Install / setup26Ubuntu, via the PostgreSQL Apt repository (from postgresql.org/download/linux/ubuntu/):2728```bash29sudo apt install -y postgresql-common30sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh31sudo apt update32sudo apt install postgresql-1833```3435The simplest path (distribution-included package) is:3637```bash38apt install postgresql39```4041## Core concepts42- Relational model with ACID transactions; everything runs inside a transaction (explicit or implicit).43- Rich type system: beyond standard SQL types, native JSON/JSONB, arrays, ranges, and user-defined types.44- MVCC (Multi-Version Concurrency Control): readers don't block writers and vice versa; old row versions are cleaned by VACUUM.45- Indexes: B-tree (default), plus GIN, GiST, BRIN, and Hash for specialized access patterns (e.g., GIN for JSONB/full-text).46- Extensions: functionality like PostGIS or pg_trgm loaded via `CREATE EXTENSION`.47- Roles and privileges: a unified role system handles both users and groups.48- Schemas: namespaces within a database for organizing objects.4950## Best practices51- Use the appropriate index type for the query; verify plans with `EXPLAIN ANALYZE` before adding indexes blindly.52- Prefer `JSONB` over `JSON` when you need to index or query inside documents (binary form supports GIN indexing).53- Let autovacuum run, and monitor for table bloat on high-churn tables (see docs on routine vacuuming).54- Use connection pooling for high-concurrency apps; each connection is a backend process.55- Define explicit constraints (NOT NULL, FOREIGN KEY, CHECK, UNIQUE) so the database enforces integrity.5657## Common pitfalls58- Skipping `EXPLAIN ANALYZE` and assuming an index is used → confirm the planner actually chooses it.59- Treating `JSON` and `JSONB` as interchangeable → `JSON` preserves text and is not efficiently indexable; use `JSONB` for querying.60- Ignoring VACUUM/autovacuum on write-heavy tables → leads to bloat and transaction ID wraparound risk.61- Opening one DB connection per request without pooling → process exhaustion under load.6263## Examples64```sql65CREATE TABLE users (66 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,67 email TEXT NOT NULL UNIQUE,68 profile JSONB NOT NULL DEFAULT '{}',69 created_at TIMESTAMPTZ NOT NULL DEFAULT now()70);7172CREATE INDEX idx_users_profile ON users USING GIN (profile);73```7475## Further reading76- Indexes: https://www.postgresql.org/docs/current/indexes.html77- Routine vacuuming: https://www.postgresql.org/docs/current/routine-vacuuming.html7879## Related skills80- ../mysql — alternative open-source RDBMS81- ../supabase — hosted Postgres with auth/storage/realtime82- ../neon — serverless Postgres83- ../cockroachdb — distributed, Postgres-wire-compatible SQL