Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Add an Index in PostgreSQL
Adding an index in PostgreSQL can dramatically improve the performance of your queries, especially on large tables. This tutorial will walk you through why indexes matter, how to create them, and some best practices.
Why Add an Index?
An index is like a roadmap for your database — it helps PostgreSQL quickly locate rows that match a query, reducing the time it takes to fetch results. Without indexes, PostgreSQL has to scan the entire table, which can be very slow.
Basic Syntax
CREATE INDEX index_name ON table_name (column_name);
Example:
CREATE INDEX idx_users_email ON users (email);
When to Use Indexes
- On columns frequently used in WHERE clauses
- On columns used in JOIN conditions
- On columns used in ORDER BY clauses
- On columns with many unique values
Types of Indexes
PostgreSQL supports several index types:
- BTREE (default): good for equality and range queries
- HASH: good for equality comparisons
- GIN: good for array, JSONB, and full-text search
- GiST: good for geometric data
Creating a Specific Index Type
CREATE INDEX index_name ON table_name USING GIN (column_name);
Example:
CREATE INDEX idx_posts_tags ON posts USING GIN (tags);
Best Practices
- Only index columns you query often — too many indexes can slow down writes.
- Use meaningful index names (e.g.,
idx_table_column
). - Regularly monitor and analyze index usage with
pg_stat_user_indexes
. - Consider partial or conditional indexes for selective queries.
Conclusion
Adding indexes is one of the easiest and most powerful ways to optimize your PostgreSQL database. Use them wisely to strike the right balance between read performance and write overhead.
For more details, check the official PostgreSQL Indexes documentation.