Rename Table in PostgreSQL

Learn how to rename a PostgreSQL table using ALTER TABLE ... RENAME TO, how dependent views and foreign keys are handled automatically, and what does not rename automatically.

4 min read · Back to overview

Quick Answer

Use ALTER TABLE table_name RENAME TO new_table_name to rename a table. PostgreSQL automatically updates foreign key constraints, views, and triggers that reference the table. Sequences and index names are NOT renamed — update them separately if naming consistency matters.

Spin up a Postgres database in 20 seconds with Vela.

Try Vela Sandbox

Renaming a table in PostgreSQL uses ALTER TABLE ... RENAME TO. Unlike some databases, PostgreSQL automatically propagates the name change to all dependent objects — foreign key constraints, views, and rules are updated transparently. The rename acquires a brief ACCESS EXCLUSIVE lock but does not rewrite any data.

Syntax

ALTER TABLE table_name RENAME TO new_table_name;

To avoid an error when the table may 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 — issue separate ALTER TABLE ... RENAME TO statements for each.

Practical Example

Create a table with dependent objects, then rename it:

CREATE TABLE suppliers (
  supplier_id  SERIAL PRIMARY KEY,
  company_name VARCHAR(200) NOT NULL,
  country      VARCHAR(100) NOT NULL
);

CREATE TABLE purchase_orders (
  po_id       SERIAL PRIMARY KEY,
  supplier_id INT NOT NULL,
  total       NUMERIC(12,2) NOT NULL,
  FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id)
    ON DELETE RESTRICT ON UPDATE CASCADE
);

CREATE VIEW supplier_summary AS
SELECT
  s.supplier_id,
  s.company_name,
  COUNT(p.po_id) AS order_count
FROM suppliers s
LEFT JOIN purchase_orders p USING (supplier_id)
GROUP BY s.supplier_id, s.company_name;

-- Rename the table
ALTER TABLE suppliers RENAME TO vendors;

After the rename, verify the foreign key and view were updated automatically:

-- Foreign key now references "vendors"
\d purchase_orders
Foreign-key constraints:
    "purchase_orders_supplier_id_fkey" FOREIGN KEY (supplier_id) REFERENCES vendors(supplier_id)
-- View definition updated automatically
\d+ supplier_summary

Check what was NOT renamed — the sequence and index keep the original prefix:

\d vendors
Default: nextval('suppliers_id_seq'::regclass)
Indexes:
    "suppliers_pkey" PRIMARY KEY, btree (supplier_id)

Rename the sequence and index separately for consistency:

ALTER SEQUENCE suppliers_id_seq RENAME TO vendors_id_seq;
ALTER INDEX suppliers_pkey RENAME TO vendors_pkey;

What Renames Automatically vs. Manually

ObjectAuto-renamed?
Foreign key references in other tablesYes
Views that join the tableYes
Rules on the tableYes
Backing SERIAL sequenceNo — rename with ALTER SEQUENCE
Primary key and unique indexesNo — rename with ALTER INDEX
Application code / ORM modelsNo — manual update required

Testing with Vela

Renaming a table is one of the riskiest schema changes for an active application — any hardcoded table name in queries, ORM models, or stored procedures will break immediately. Vela database branching lets you rename the table on a branch and point your test environment at it to surface every reference that needs updating before the rename hits production.

Production Tips

  • Renaming a table is metadata-only — no data is rewritten, making it fast even on very large tables.
  • Application code and ORM models that reference the old table name will break immediately after the rename. Update and deploy them in the same release window.
  • SERIAL sequences keep the old name prefix after the rename. Rename them with ALTER SEQUENCE old_seq RENAME TO new_seq if naming consistency matters.
  • Index names also retain the original prefix. Rename with ALTER INDEX old_idx RENAME TO new_idx.
  • Always test renames in a staging environment or Vela branch before applying to production.

Continue in Managing Tables: Add Column.

Related in this section: PostgreSQL Data Types · Create Table · Select Into

Frequently Asked Questions

What does ALTER TABLE RENAME TO do in PostgreSQL?
ALTER TABLE RENAME TO changes the table's name in the system catalog without touching any data. PostgreSQL automatically updates dependent views, foreign key constraints, and rules to reference the new name. The table's internal object ID (OID) is unchanged.
Does renaming a table lock the table and block queries?
Yes, briefly. ALTER TABLE RENAME TO acquires an ACCESS EXCLUSIVE lock for the duration of the rename, blocking concurrent reads and writes. Because the operation only updates the system catalog (no data is rewritten), the lock is held for a very short time.
What happens to sequences and indexes when I rename a table?
They are NOT automatically renamed. A SERIAL column's backing sequence (e.g., old_table_id_seq) keeps the original table name prefix. Primary key and unique indexes also keep their original names. Rename them separately with ALTER SEQUENCE and ALTER INDEX if consistency is important.
Can I use IF EXISTS with ALTER TABLE RENAME TO?
Yes. ALTER TABLE IF EXISTS table_name RENAME TO new_name issues a NOTICE rather than an error when the table does not exist. This is useful in idempotent migration scripts.
What is the safest way to rename a table in production?
Test the rename on a Vela branch first. Update all application code, ORM models, stored procedures, and migration scripts that reference the old name. Rename during a low-traffic window and deploy the updated application immediately after.