This article mainly focus on how to rename table in PostgreSQL Database. It is one of the most basic task exist in every type of database. In this context, the database for further demonstration in order to perform the task is the PostgreSQL Database. That task is renaming a table exist inside a database in the PostgreSQL Database. There are several steps which is important in order to achieve the purpose. The following are the description of those steps :
1. Connect to PostgreSQL Database Server by executing the following command :
user@hostname:~$ psql -Upostgres postgres psql (10.5) Type "help" for help. user=#
2. After successfully connect to the PostgreSQL Database Server using the correct parameter, execute the following command to list the available tables. Using the correct parameter means using the correct username and the correct database. The command for listing the available tables as follows :
postgres=# \dt+ List of relations Schema | Name | Type | Owner | Size | Description --------+--------------------------+-------+----------+------------+------------- public | table_test | table | postgres | 16 kB | ... (25 rows) postgres=#
So, by executing the above command, the ‘\d+’ in the PostgreSQL Command Console, it will actually display the list of available tables in the current database connected.
3. After successfully listing the tables available in the database, just execute the following command to rename the correct table :
postgres=# alter table table_test rename to test; ALTER TABLE postgres=#
4. Don’t forget to list the table again. It is an important step to make sure that the table name has changed.
postgres=# \dt+ List of relations Schema | Name | Type | Owner | Size | Description --------+--------------------------+-------+----------+------------+------------- public | test | table | postgres | 16 kB | ... (xx rows) postgres=#
If it has already changed, it means the above query or command for changing or renaming the name of the table is actually works. According to the output above, the name of the table has already changed. The table name is now ‘test’. As in the previous output displays, the previous table name is ‘table_test’.