

Postgresql rename table how to#
How to Use RENAME COLUMN Command to Rename Several Columns?įollow the semi-colon separated syntax to rename several columns in PostgreSQL: ALTER TABLE tab1_nameĮxample: How to Rename Several Columns in Postgres? The above snippet clarified that the “article_id” had been renamed to “id” in the selected table as well as in the dependent objects/tables. Type “\d” command followed by the table name (i.e., “article_detials”) to see the changes made in the selected table: \d article_detials Let’s run the below-given command to rename the “article_id” column to “id”: ALTER TABLE article_details So, renaming the “article_id” column in the “article_details” table will automatically rename the “article_id” column in the “author_details” table. The above snippet shows that the “article_id” is a foreign key in the “author_details” table. Renaming a column that has dependent objects such as foreign keys, views, stored procedures, etc., will implement the modifications to its dependent objects as well.Įxample: How to Rename a Foreign Key in PostgreSQL?Ĭonsider the below-given steps to rename a column on which some other objects are dependent:įirstly, we will fetch the “article_details” and “author_details” tables using the “\d” command: How to Rename a Column That Has Some Dependent Objects? The output verified that the “staff_location” column had been renamed to “staff_address”. Let’s execute the “\d” command followed by the table name to verify whether the column name has been altered or not: \d staff_details Execute the “RENAME COLUMN” command to rename the selected column: ALTER TABLE staff_details RENAME COLUMN staff_location TO staff_address Suppose we have to rename the “staff_location” column to “staff_address”. Let’s run the “\d” command followed by the table name to see all the columns present in the selected table: \d staff_details Select a table of your choice from the available tables. Run the “\dt” command to check the available tables within the connected database: \dt The output shows that we are successfully connected to the “example” database. Open the SQL SHELL and establish a connection with a database using the “\c” command: \c example new_col_name represents new/modified column name.Įxample # 1: How to Rename a Table’s Column in Postgres?įollow the below-given steps to learn how RENAME COLUMN command works in PostgreSQL: old_col_name represents a column to be renamed.

RENAME COLUMN is a command that renames a column.

tab_name is a table to be altered/modified. ALTER TABLE is a clause used to alter or modify a table. Let’s understand how the above snippet works: RENAME COLUMN old_col_name TO new_col_name The RENAME COLUMN command gets executed with the assistance of ALTER TABLE command, as shown in the following syntax: ALTER TABLE tab_name The “RENAME COLUMN” command can also be used as “RENAME”. How to Use RENAME COLUMN Command to Rename Columns in PostgreSQL? How to Use RENAME COLUMN Command to Rename Several Columns in PostgreSQL?.How to Rename a Column That Has Some Dependent Objects?.How to Use RENAME COLUMN Command to Rename Columns in PostgreSQL?.This write-up will explain the below-listed use-cases of the RENAME COLUMN command in PostgreSQL. PostgreSQL doesn’t provide the “IF EXISTS” option for the “RENAME COLUMN” command. The RENAME COLUMN command allows us to rename a single or multiple columns. PostgreSQL provides a RENAME COLUMN clause that is used with the collaboration of ALTER TABLE command to rename a column.
