> This page location: Managing Tables > Rename Table
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL Rename Table: A Step-by-Step Guide

**Summary**: in this tutorial, you will learn how to rename a table using the PostgreSQL `ALTER TABLE RENAME TO` statement.

## Introduction to PostgreSQL RENAME TABLE statement

To change the name of an existing table, you use the `ALTER TABLE... RENAME TO` statement as follows:

```sql
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, assign the new table name after the `RENAME TO` clause.

If you rename a table that does not exist, PostgreSQL will issue an error.

To avoid the error, you can use the `IF EXISTS` option:

```sql
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 rename multiple tables using a single `ALTER TABLE` statement.

## PostgreSQL rename table examples

Let's take some examples of using the `ALTER TABLE ... RENAME TO` statement.

### 1) Basic PostgreSQL rename table statement example

First, [create a new table](https://neon.com/postgresql/postgresql-tutorial/postgresql-create-table) called `vendors` for the demonstration purpose:

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

Second, describe the `vendors` table using the `\d` command in `psql`:

```text
\d vendors
```

Output:

```
                                 Table "public.vendors"
 Column |       Type        | Collation | Nullable |               Default
--------+-------------------+-----------+----------+-------------------------------------
 id     | integer           |           | not null | nextval('vendors_id_seq'::regclass)
 name   | character varying |           | not null |
Indexes:
    "vendors_pkey" PRIMARY KEY, btree (id)
```

Third, change the name of the `vendors` table to `suppliers` using the `ALTER TABLE...RENAME TO` statement:

```sql
ALTER TABLE vendors
RENAME TO suppliers;
```

Finally, describe the `suppliers` table:

```text
                                Table "public.suppliers"
 Column |       Type        | Collation | Nullable |               Default
--------+-------------------+-----------+----------+-------------------------------------
 id     | integer           |           | not null | nextval('vendors_id_seq'::regclass)
 name   | character varying |           | not null |
Indexes:
    "vendors_pkey" PRIMARY KEY, btree (id)
```

Notice that the name of the table changed but the [sequence](https://neon.com/postgresql/postgresql-tutorial/postgresql-sequences) (`vendors_id_seq`) remains intact.

### 1) Renaming a table that has dependent objects

First, create new tables called `customers` and `groups`:

```sql
CREATE TABLE customer_groups(
    id SERIAL PRIMARY KEY,
    name VARCHAR NOT NULL
);

CREATE TABLE customers(
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    group_id INT NOT NULL,
    FOREIGN KEY (group_id) REFERENCES customer_groups(id)
       ON DELETE CASCADE
       ON UPDATE CASCADE
);
```

Second, [create a view](../postgresql-views/postgresql-materialized-views) based on the `customers` and `customer_groups` tables:

```sql
CREATE VIEW customer_data
AS SELECT
    c.id,
    c.name,
    g.name customer_group
FROM
    customers c
INNER JOIN customer_groups g ON g.id = c.group_id;
```

When you rename a table, PostgreSQL will automatically update its dependent objects such as [foreign key constraints](https://neon.com/postgresql/postgresql-tutorial/postgresql-foreign-key), [views](../postgresql-views), and [indexes](../postgresql-indexes).

Third, rename the `customer_groups` table to `groups`:

```sql
ALTER TABLE customer_groups
RENAME TO groups;
```

Fourth, verify the foreign key constraint in the `customers` table by showing the table via `\d` command in `psql`:

```
\d customers
```

Output:

```
                                     Table "public.customers"
  Column  |          Type          | Collation | Nullable |                Default
----------+------------------------+-----------+----------+---------------------------------------
 id       | integer                |           | not null | nextval('customers_id_seq'::regclass)
 name     | character varying(255) |           | not null |
 group_id | integer                |           | not null |
Indexes:
    "customers_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "customers_group_id_fkey" FOREIGN KEY (group_id) REFERENCES groups(id) ON UPDATE CASCADE ON DELETE CASCADE
```

The output indicates that the foreign key constraint was updated and referenced the `groups` table instead of the `customer_groups` table.

Fifth, show the `customer_data` view in psql:

```
\d+ customer_data
```

Output:

```
                                    View "public.customer_data"
     Column     |          Type          | Collation | Nullable | Default | Storage  | Description
----------------+------------------------+-----------+----------+---------+----------+-------------
 id             | integer                |           |          |         | plain    |
 name           | character varying(255) |           |          |         | extended |
 customer_group | character varying      |           |          |         | extended |
View definition:
 SELECT c.id,
    c.name,
    g.name AS customer_group
   FROM customers c
     JOIN groups g ON g.id = c.group_id;
```

The output shows that the `customer_groups` table in the [`SELECT`](https://neon.com/postgresql/postgresql-tutorial/postgresql-select) statement of the view was also updated to `groups` table.

## Summary

- Use the `ALTER TABLE ... RENAME TO` statement to rename a table.

---

## Related docs (Managing Tables)

- [PostgreSQL Data Types](https://neon.com/postgresql/postgresql-tutorial/postgresql-data-types)
- [Create Table](https://neon.com/postgresql/postgresql-tutorial/postgresql-create-table)
- [Select Into](https://neon.com/postgresql/postgresql-tutorial/postgresql-select-into)
- [Create Table As](https://neon.com/postgresql/postgresql-tutorial/postgresql-create-table-as)
- [SERIAL](https://neon.com/postgresql/postgresql-tutorial/postgresql-serial)
- [Sequences](https://neon.com/postgresql/postgresql-tutorial/postgresql-sequences)
- [Identity Column](https://neon.com/postgresql/postgresql-tutorial/postgresql-identity-column)
- [Generated Columns](https://neon.com/postgresql/postgresql-tutorial/postgresql-generated-columns)
- [Alter Table](https://neon.com/postgresql/postgresql-tutorial/postgresql-alter-table)
- [Add Column](https://neon.com/postgresql/postgresql-tutorial/postgresql-add-column)
- [Drop Column](https://neon.com/postgresql/postgresql-tutorial/postgresql-drop-column)
- [Change Column's Data Type](https://neon.com/postgresql/postgresql-tutorial/postgresql-change-column-type)
- [Rename Column](https://neon.com/postgresql/postgresql-tutorial/postgresql-rename-column)
- [Drop Table](https://neon.com/postgresql/postgresql-tutorial/postgresql-drop-table)
- [Temporary Table](https://neon.com/postgresql/postgresql-tutorial/postgresql-temporary-table)
- [Truncate Table](https://neon.com/postgresql/postgresql-tutorial/postgresql-truncate-table)
- [Copy Table](https://neon.com/postgresql/postgresql-tutorial/postgresql-copy-table)
