CREATE INDEX (Create Index)

Last updated: 2026-11-04

CREATE INDEX (Create Index)

CREATE INDEX builds an index on a table or materialized view so queries avoid full scans. It supports several access methods — B-tree, hash, GiST, GIN, BRIN — and can define unique, partial, expression, and multicolumn indexes. Indexes speed up reads but must be maintained on every write, so build them only on columns that queries really use.

Kindcommand
Example LanguageSQL

📝 Syntax

CREATE [UNIQUE] INDEX [CONCURRENTLY] [IF NOT EXISTS] <index name>
ON <table> [USING <access method>]
(<column> | (<expression>) [, ...])
[INCLUDE (<column>, ...)]
[WHERE <partial index predicate>]

⚙️ Parameters

UNIQUEIndex values must be unique; also acts as a unique constraint
USINGAccess method; btree by default
INCLUDEExtra columns stored in the index but not used for ordering
WHEREPartial-index predicate; only matching rows are indexed
CONCURRENTLYBuilds the index without blocking reads or writes

Returns:No return value. Once it succeeds the index shows up in pg_indexes.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

DROP TABLE
CREATE TABLE
INSERT 0 3
CREATE INDEX
CREATE INDEX
CREATE INDEX
      indexname      
---------------------
customers_age_partial
customers_city_idx   
customers_email_uniq 
customers_pkey

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QShould I add as many indexes as possible?
ANo. Every index slows down INSERT/UPDATE/DELETE and consumes disk. Index only the columns that frequent queries filter, join, and sort on, and prune indexes that go unused.
QWhen should I use CONCURRENTLY?
AUse it when adding an index to a large table in production: it does not block reads or writes, at the cost of a longer run and an INVALID index left behind on failure that must be cleaned up by hand.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏