ALTER TABLE … RENAME TO changes a table’s name while preserving all data, indexes, and privileges.
Teams rename tables to match new business terms, phase out legacy names, or merge schemas. Doing so keeps data, indexes, constraints, and privileges intact, avoiding the cost of creating a new table and re-migrating data.
Use ALTER TABLE [IF EXISTS] current_name RENAME TO new_name;
. Add IF EXISTS
to suppress errors if the table is missing. Only the owning role or a superuser can run this command.
ALTER TABLE Orders RENAME TO CustomerOrders;
The command runs instantly because PostgreSQL updates only the system catalog.
Yes. Wrap several ALTER TABLE ... RENAME
statements in a BEGIN; … COMMIT;
block to keep the schema consistent if any step fails.
PostgreSQL automatically updates foreign keys, sequences, and constraint names that reference the old table. Views and functions using the old name are not updated; refresh them manually.
customer_orders
vs custord
).Although instant, the command still takes an ACCESS EXCLUSIVE
lock. Schedule it during maintenance windows.
Views break after the rename. Recreate or refresh them immediately.
1) Confirm no conflicting migrations. 2) Search and replace old table name in code, tests, and dashboards. 3) Add the change to version control. 4) Run in a transaction when possible.
No. PostgreSQL only updates catalog entries, so the operation is nearly instantaneous.
Yes. If the command is inside an open transaction, a simple ROLLBACK;
restores the original name.
Yes. All GRANTs on the table persist after renaming.