How to rename PostgreSQL Table

In this article, we will see how to rename PostgreSQL Table. ALTER TABLE is the command used to rename table in PostgreSQL Server. To execute ALTER TABLE, we must be owner of table or superuser or granted alter table permission on the table.

PostgreSQL Rename Table Syntax:

ALTER TABLE name RENAME TO new_name

1. Lets verify the list of tables.

\dt+

r2schools=# \dt; List of relations Schema | Name | Type | Owner --------+----------+-------+------------ public | class | table | postgres public | dept | table | postgres public | emp | table | postgres public | employee | table | postgres public | rr | table | salesadmin public | student | table | postgres public | students | table | postgres

2. Lets rename table rr to reader in PostgreSQL.

ALTER TABLE rr RENAME TO reader;

3. Lets verify the list of tables.

r2schools=# \dt; List of relations Schema | Name | Type | Owner --------+----------+-------+------------ public | class | table | postgres public | dept | table | postgres public | emp | table | postgres public | employee | table | postgres public | reader | table | salesadmin public | student | table | postgres public | students | table | postgres