RENAME INDEX

2020-02-16

The RENAME INDEX statement changes the name of an index for a table.

{{site.data.alerts.callout_info}}It is not possible to rename an index referenced by a view. For more details, see View Dependencies.{{site.data.alerts.end}}

Synopsis

{% include {{ page.version.version }}/sql/diagrams/rename_index.html %}

Required privileges

The user must have the CREATE privilege on the table.

Parameters

Parameter Description
IF EXISTS Rename the column only if a column of current_name exists; if one does not exist, do not return an error.
table_name The name of the table with the index you want to use
index_name The current name of the index
name The name you want to use for the index, which must be unique to its table and follow these identifier rules.

Example

Rename an Index

> SHOW INDEXES FROM users;
+------------+------------+------------+--------------+-------------+-----------+---------+----------+
| table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit |
+------------+------------+------------+--------------+-------------+-----------+---------+----------+
| users      | primary    |   false    |            1 | id          | ASC       |  false  |  false   |
| users      | name_idx   |    true    |            1 | name        | ASC       |  false  |  false   |
| users      | name_idx   |    true    |            2 | id          | ASC       |  false  |   true   |
+------------+------------+------------+--------------+-------------+-----------+---------+----------+
(3 rows)
> ALTER INDEX users@name_idx RENAME TO users_name_idx;
> SHOW INDEXES FROM users;
+------------+----------------+------------+--------------+-------------+-----------+---------+----------+
| table_name |   index_name   | non_unique | seq_in_index | column_name | direction | storing | implicit |
+------------+----------------+------------+--------------+-------------+-----------+---------+----------+
| users      | primary        |   false    |            1 | id          | ASC       |  false  |  false   |
| users      | users_name_idx |    true    |            1 | name        | ASC       |  false  |  false   |
| users      | users_name_idx |    true    |            2 | id          | ASC       |  false  |   true   |
+------------+----------------+------------+--------------+-------------+-----------+---------+----------+
(3 rows)

See also