Renaming a table in PostgreSQL is a single-statement operation using ALTER TABLE ... RENAME TO. Unlike some databases, PostgreSQL automatically propagates the name change to all dependent objects — foreign key constraints, views, and indexes are updated to reference the new table name. The rename acquires an ACCESS EXCLUSIVE lock on the table but does not rewrite any data.
RENAME TABLE syntax
ALTER TABLE table_name RENAME TO new_table_name;
To avoid an error if the table does not exist:
ALTER TABLE IF EXISTS table_name RENAME TO new_table_name;
There is no syntax to rename multiple tables in a single statement — use separate ALTER TABLE ... RENAME TO statements for each.
Rename table examples
Example 1 — basic rename:
CREATE TABLE vendors (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL
);
ALTER TABLE vendors RENAME TO suppliers;
Inspect the renamed table in psql:
d suppliers
Table "public.suppliers"
Column | Type | Nullable | Default
--------+-------------------+----------+-------------------------------------
id | integer | not null | nextval('vendors_id_seq'::regclass)
name | character varying | not null |
Indexes:
"vendors_pkey" PRIMARY KEY, btree (id)
Notice the sequence (vendors_id_seq) and the index name (vendors_pkey) retain the original names. Only the table name itself changes.
Example 2 — rename a table that has dependent objects:
CREATE TABLE customer_groups (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL
);
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
group_id INT NOT NULL,
FOREIGN KEY (group_id) REFERENCES customer_groups (id)
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE VIEW customer_data AS
SELECT c.id, c.name, g.name AS customer_group
FROM customers c
INNER JOIN customer_groups g ON g.id = c.group_id;
-- Rename the referenced table
ALTER TABLE customer_groups RENAME TO groups;
After the rename, both the foreign key and the view are automatically updated:
-- Foreign key now references "groups"
d customers
...
Foreign-key constraints:
"customers_group_id_fkey" FOREIGN KEY (group_id) REFERENCES groups(id)
-- View definition updated automatically
d+ customer_data
...
FROM customers c
JOIN groups g ON g.id = c.group_id;
Rename table tips
- The sequence created by
SERIALkeeps its original name (e.g.,old_table_id_seq) after the table rename. If naming consistency matters, rename the sequence separately withALTER SEQUENCE old_seq RENAME TO new_seq. - Existing index names also retain the original table name prefix. Rename them with
ALTER INDEX old_index_name RENAME TO new_index_nameif desired. - Always test renames in a staging environment first — application code or stored procedures that reference the old table name will break and must be updated.
- You cannot rename multiple tables in a single statement. Each rename requires a separate
ALTER TABLEcall.
Reference: PostgreSQL documentation — ALTER TABLE.