How to rename table in PostgreSQL

Galaxy Glossary

How do I rename a table in PostgreSQL without losing data?

ALTER TABLE … RENAME TO changes the name of an existing PostgreSQL table without touching the data or permissions.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why would you rename a PostgreSQL table?

Renaming helps reflect new business logic, fix typos, merge schemas, or follow naming conventions without copying data. The ALTER TABLE … RENAME TO statement makes the change instantly and keeps all data intact.

How do I rename a table in PostgreSQL?

Run ALTER TABLE current_name RENAME TO new_name;. You need ownership or super-user rights. The command is transactional—if it fails, nothing changes.

Basic rename example

ALTER TABLE sales RENAME TO sales_archive;

The table sales becomes sales_archive. All rows, constraints, indexes, and ACLs move with it.

Will dependent objects update automatically?

Foreign-key constraints, sequences owned by serial/bigserial columns, and views referencing the table by OID stay valid. Views/functions that hard-code the table name must be recreated.

What is the exact syntax of ALTER TABLE … RENAME?

ALTER TABLE [IF EXISTS] schema_old.table_old
RENAME TO table_new;

Parameters

  • IF EXISTS – skip error if the table is missing.
  • schema_old.table_old – current qualified name.
  • table_new – new unqualified name, unique inside the schema.

Best practices for renaming tables

Schedule during low traffic windows if downstream tools cache metadata. Wrap the rename and view/function refreshes in one transaction. Qualify table names to avoid search_path surprises. Announce changes in shared docs or Galaxy Collections.

Common mistakes to avoid

Using DROP & CREATE instead of RENAME. This deletes data and privileges. Always use ALTER TABLE … RENAME TO.

Forgetting schema qualification. If you rely on search_path, you might rename the wrong table. Use ALTER TABLE analytics.sales RENAME TO sales_q1;.

More rename examples

-- Rename only if the table exists
ALTER TABLE IF EXISTS public.temp_users RENAME TO users_staging;

-- Rename inside a transaction with dependent view
BEGIN;
ALTER TABLE payments RENAME TO payments_2023;
CREATE OR REPLACE VIEW payments AS SELECT * FROM payments_2023;
COMMIT;

Related commands

  • ALTER TABLE … RENAME COLUMN – rename a column
  • ALTER SEQUENCE … RENAME TO – rename a sequence
  • ALTER VIEW … RENAME TO – rename a view

Why How to rename table in PostgreSQL is important

How to rename table in PostgreSQL Example Usage


ALTER TABLE IF EXISTS public.customers RENAME TO customers_2024;

How to rename table in PostgreSQL Syntax


ALTER TABLE [IF EXISTS] <schema_name>.<old_table_name>
    RENAME TO <new_table_name>;

Common Mistakes

Frequently Asked Questions (FAQs)

Does renaming a table lock it?

PostgreSQL takes an Access Exclusive lock, but only for a moment. Reads continue; concurrent writes and DDL wait briefly.

Will views and foreign keys break after renaming?

Foreign keys update automatically. Views remain valid if they reference the table’s OID, but views/functions that hard-code the name must be recreated.

Can I rename a table in another schema?

Yes. Qualify the current table name: ALTER TABLE old_schema.orders RENAME TO orders_2024;. The table stays in the same schema.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.