0

How to add an index to an existing table in PostgreSQL

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

Step 1: 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);

Comments