Skip to content
Related Articles

Related Articles

PostgreSQL – Rename Table
  • Last Updated : 28 Aug, 2020

PostgreSQL has a RENAME clause that is used with the ALTER TABLE statement to rename the name of an existing table.

Syntax:
ALTER TABLE table_name
RENAME TO new_table_name; 

In the above syntax:

  • First, specify the name of the table which you want to rename after the ALTER TABLE clause.
  • Second, give the new table name after the RENAME TO clause.

Attempting to rename a table that doesn’t exist leads to PostgreSQL raising an error. To rename multiple tables, one has to execute multiple ALTER TABLE RENAME TO statements. It’s not possible to do it in a single statement.
For the purpose of example let’s first create a table using the below statements and then we will attempt to rename it:

CREATE TABLE vendors (
    id serial PRIMARY KEY,
    name VARCHAR NOT NULL
);

Now check if the table has been successfully created using the below statement:

SELECT * FROM vendors;

Now that our table is ready, let’s jump into an example.



Example:
In this we will rename the vendors table to suppliers, using the following ALTER TABLE RENAME TO statement:

ALTER TABLE vendors RENAME TO suppliers;

Now we can verify if the name has been changed using the below statement:

SELECT * FROM suppliers;

Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :