Skip to main content
0 reactions

How to Rename a Table in PostgreSQL

Featured card for How to Rename a Table in PostgreSQL, with a terminal showing ALTER TABLE clients RENAME TO customers, the table listed as customers, and IF EXISTS with no error.
How to Rename a Table in PostgreSQL

To rename a table in PostgreSQL, use ALTER TABLE … RENAME TO with the new name. The rows, the columns and everything else in the table stay as they were. Here’s how, and how to avoid an error when the table may not be there.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
ALTER TABLE [ IF EXISTS ] table_name
    RENAME TO new_name;
  • table_name: the table’s current name.
  • new_name: its new name. No other table in the same schema may have it already.
  • IF EXISTS: optional. If there’s no such table, skip it with a notice instead of an error.
  • [ ] marks the optional part.

See ALTER TABLE (opens in a new tab) in the PostgreSQL documentation.

The Example

An office keeps its clients in a table named clients, and the team now calls them customers.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE office;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c office
OutputPlain text
You are now connected to database "office" as user "postgres".

Step 2: Create the Table and Add a Client

office=#SQL
CREATE TABLE clients (name text);
OutputPlain text
CREATE TABLE
office=#SQL
INSERT INTO clients (name) VALUES ('Blue Fern Studio');
OutputPlain text
INSERT 0 1

Step 3: Rename the Table

office=#SQL
ALTER TABLE clients RENAME TO customers;
OutputPlain text
ALTER TABLE

The table is listed under its new name:

office=#Plain text
\dt
OutputPlain text
            List of tables
 Schema |   Name    | Type  |  Owner
--------+-----------+-------+----------
 public | customers | table | postgres
(1 row)

And its row came along:

office=#SQL
SELECT * FROM customers;
OutputPlain text
       name
------------------
 Blue Fern Studio
(1 row)

Step 4: Rename Only If the Table Exists

Running the rename again fails, because clients is gone:

office=#SQL
ALTER TABLE clients RENAME TO customers;
OutputPlain text
ERROR:  relation "clients" does not exist

IF EXISTS turns the error into a notice, which helps in a script that may run twice:

office=#SQL
ALTER TABLE IF EXISTS clients RENAME TO customers;
OutputPlain text
NOTICE:  relation "clients" does not exist, skipping
ALTER TABLE

Clean Up

To remove the example, connect to another database and drop the test database:

office=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE office;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Renaming to a name another table already hasERROR: relation "customers" already existsPick another name, or rename or drop the other table first.
Renaming a table that doesn’t exist, for example twiceERROR: relation "clients" does not existCheck the names with \dt, or add IF EXISTS.
Forgetting the queries that use the old nameThey fail, because no table has that name any more.Update them in the same change.

FAQ

Does renaming a table copy its data?

No. Only the name changes; the rows and columns stay where they are.

Conclusion

ALTER TABLE … RENAME TO gives a table a new name and keeps everything in it. Add IF EXISTS when the table may not be there, and update the queries that use the old name.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments