How to add an index to an existing table in PostgreSQL

aaron's profile image
By aaron

To add an index to an existing table in PostgreSQL, do the following.

## Use the CREATE INDEX command

Use the CREATE INDEX command:

CREATE INDEX idx_myindex ON table_name(column_name);

For example, on a table of users:

CREATE INDEX idx_usernames ON users(username);

To create a UNIQUE index, add the UNIQUE keyword. Example:

CREATE UNIQUE INDEX idx_usernames ON users(username);

Support the author

This author accepts donations via the services listed below. Your donation will help them continue to create great content!

* Lernabit doesn't take any of the money from your donation, but the donation services or payment processors might take a fee. These trademarks are the property of their respective owners.

Replies

Login or signup to leave a reply.

Signup Login
No more replies to show here