PostgreSQL – How to Rename a Column
To rename a column in PostgreSQL, you can use the ALTER TABLE
statement with the RENAME COLUMN
clause. Below is the basic syntax for renaming a column in PostgreSQL:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
where table_name
is the name of the table containing the column you want to rename, old_column_name
is the current name of the column, and new_column_name
is the new name you want to give to the column.
Here’s an example of how to use this syntax:
Consider a table called “employees” with a column named “emp_name,” and you want to rename it to “employee_name.” You would execute the following SQL statement:
ALTER TABLE employees RENAME COLUMN emp_name TO employee_name;
After running this SQL statement, the “emp_name” column in the “employees” table will be renamed to “employee_name.” Make sure to back up your data and use caution when renaming columns, especially in production databases, as it can affect the functionality of applications that rely on the table’s structure.