PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL Rename Table: A Step-by-Step Guide

PostgreSQL Rename Table: A Step-by-Step Guide

Summary: this tutorial shows you how to rename a table by using the PostgreSQL RENAME table clause of the  ALTER TABLE statement.

Overview of PostgreSQL rename table statement

To rename an existing table, you use the ALTER TABLE statement as follows:

1
2
ALTER TABLE table_name
RENAME TO new_table_name;

In this statement:

  • 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.

If you try to rename a table that does not exist, PostgreSQL will issue an error. To avoid this, you add the IF EXISTS option as follows:

1
2
ALTER TABLE IF EXISTS table_name
RENAME TO new_table_name;

In this case, if the table_name does not exist, PostgreSQL will issue a notice instead.

To rename multiple tables, you have to execute multiple ALTER TABLE RENAME TO statements. It’s not possible to do it in a single statement.

PostgreSQL rename table examples

We create a new table named vendors for the sake of demonstration.

1
2
3
4
CREATE TABLE vendors (
    id serial PRIMARY KEY,
    name VARCHAR NOT NULL
);

To rename the vendors table to suppliers, you use the following ALTER TABLE RENAME TO statement:

1
ALTER TABLE vendors RENAME TO suppliers;

Suppose each vendor or supplier belongs to a group. To manage this relationship, we need to add the supplier_groups table as follows:

1
2
3
4
CREATE TABLE supplier_groups (
    ID serial PRIMARY KEY,
    name VARCHAR NOT NULL
);

We also need to add a new column to the suppliers table named group_id. This column is the foreign key column that links to the id column of the supplier_groups table.

1
2
3
4
5
ALTER TABLE suppliers
ADD COLUMN group_id INT NOT NULL;
 
ALTER TABLE suppliers
ADD FOREIGN KEY (group_id) REFERENCES supplier_groups (ID);

To save time querying complete supplier data, we create a view against the suppliers and supplier_groups tables as follows:

1
2
3
4
5
6
7
CREATE VIEW supplier_data AS SELECT
    s.id,
    s.name,
    g.name group
FROM
    suppliers s
INNER JOIN supplier_groups g ON g.id = s.group_id;

When you rename a table to the new one, PostgreSQL will automatically update its dependent objects such as foreign key constraints, views, and indexes, etc.

Let’s check the suppliers table first:

1
2
3
4
5
6
7
8
9
10
11
test=# \d suppliers;
                              Table "public.suppliers"
  Column  |       Type        |                      Modifiers
----------+-------------------+------------------------------------------------------
id       | integer           | not null default nextval('vendors_id_seq'::regclass)
name     | character varying | not null
group_id | integer           | not null
Indexes:
    "vendors_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "suppliers_group_id_fkey" FOREIGN KEY (group_id) REFERENCES supplier_groups(id)

The output shows that the suppliers table has a foreign key constraint which references to the supplier_groups table.

Now, we rename the supplier_groups table to groups as follows:

1
ALTER TABLE supplier_groups RENAME TO groups;

You can verify the foreign key constraint in the suppliers table by describing the suppliers table as follows:

1
2
3
4
5
6
7
8
9
10
11
test-# \d suppliers;
                              Table "public.suppliers"
  Column  |       Type        |                      Modifiers
----------+-------------------+------------------------------------------------------
id       | integer           | not null default nextval('vendors_id_seq'::regclass)
name     | character varying | not null
group_id | integer           | not null
Indexes:
    "vendors_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "suppliers_group_id_fkey" FOREIGN KEY (group_id) REFERENCES groups(id)

As you can see, the foreign key constraint was updated and referenced to the groups table instead.

The following statement shows the supplier_data view:

1
2
3
4
5
6
7
8
9
10
11
12
13
test-# \d+ supplier_data;
                       View "public.supplier_data"
     Column     |       Type        | Modifiers | Storage  | Description
----------------+-------------------+-----------+----------+-------------
id             | integer           |           | plain    |
name           | character varying |           | extended |
supplier_group | character varying |           | extended |
View definition:
SELECT s.id,
    s.name,
    g.name AS supplier_group
   FROM suppliers s
     JOIN groups g ON g.id = s.group_id;

The output shows that the supplier_groups table in the SELECT statement of the view was also updated to groups table.

In this tutorial, you have learned how to rename a table to the new one by using the PostgreSQL RENAME table clause of the ALTER TABLE statement.

Previous Tutorial: PostgreSQL ALTER TABLE
Next Tutorial: PostgreSQL ADD COLUMN: Add One Or More Columns To a Table

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.