9951 explained code solutions for 126 technologies


postgresqlHow do I create an index in PostgreSQL?


Creating an index in PostgreSQL is quite simple.

  1. First, you will need to write an SQL statement that will create the index. For example, to create an index on a column called "id" in a table called "users":
CREATE INDEX users_id_idx ON users (id);
  1. Then, execute the statement using an SQL client or the psql command line tool:
psql> CREATE INDEX users_id_idx ON users (id);
CREATE INDEX
  1. You can check if the index was created by running the \d command:
psql> \d users
                               Table "public.users"
   Column   |          Type          | Collation | Nullable | Default
------------+------------------------+-----------+----------+---------
 id         | integer                |           | not null |
...
Indexes:
    "users_id_idx" btree (id)
  1. The index can be dropped by running the DROP INDEX command:
DROP INDEX users_id_idx;
  1. You can also create a unique index, which ensures that no two rows have the same value in the indexed column:
CREATE UNIQUE INDEX users_id_idx ON users (id);
  1. You can also create an index on multiple columns:
CREATE INDEX users_firstname_lastname_idx ON users (first_name, last_name);
  1. For more information, see the PostgreSQL documentation.

Edit this code on GitHub