9951 explained code solutions for 126 technologies


postgresqlHow do I create an index in PostgreSQL?


Creating an index in PostgreSQL is a simple process. First, you must specify the table and column you wish to create the index on:

CREATE INDEX index_name ON table_name (column_name);

This will create an index on the specified table and column. You can also specify multiple columns, if desired:

CREATE INDEX index_name ON table_name (column_name1, column_name2);

If you wish to create a unique index, you can specify the UNIQUE keyword:

CREATE UNIQUE INDEX index_name ON table_name (column_name);

You can also specify a name for the index:

CREATE INDEX index_name ON table_name (column_name) USING btree (index_name);

This will create an index on the specified table and column with the specified name.

You can also specify sorting order:

CREATE INDEX index_name ON table_name (column_name) USING btree (index_name) ORDER BY column_name;

This will create an index on the specified table and column with the specified name and sorting order.

Helpful links

Edit this code on GitHub