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
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:
CREATE DATABASE office;
CREATE DATABASE
\c office
You are now connected to database "office" as user "postgres".
Step 2: Create the Table and Add a Client
CREATE TABLE clients (name text);
CREATE TABLE
INSERT INTO clients (name) VALUES ('Blue Fern Studio');
INSERT 0 1
Step 3: Rename the Table
ALTER TABLE clients RENAME TO customers;
ALTER TABLE
The table is listed under its new name:
\dt
List of tables
Schema | Name | Type | Owner
--------+-----------+-------+----------
public | customers | table | postgres
(1 row)
And its row came along:
SELECT * FROM customers;
name
------------------
Blue Fern Studio
(1 row)
Step 4: Rename Only If the Table Exists
Running the rename again fails, because clients is gone:
ALTER TABLE clients RENAME TO customers;
ERROR: relation "clients" does not exist
IF EXISTS turns the error into a notice, which helps in a script that may run twice:
ALTER TABLE IF EXISTS clients RENAME TO customers;
NOTICE: relation "clients" does not exist, skipping
ALTER TABLE
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE office;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Renaming to a name another table already has | ERROR: relation "customers" already exists | Pick another name, or rename or drop the other table first. |
| Renaming a table that doesn’t exist, for example twice | ERROR: relation "clients" does not exist | Check the names with \dt, or add IF EXISTS. |
| Forgetting the queries that use the old name | They 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
- PostgreSQL 19 documentation: ALTER TABLE (opens in a new tab)
- PostgreSQL 19 documentation: Renaming a Table (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 6: Create Tables
- How to Drop a Table in PostgreSQL
- How to List Tables in PostgreSQL
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