Rename Column of a Table in PostgreSQL
ALTER TABLE RENAME COLUMN statement to change name of column.
ALTER TABLE [schema_name.]table_name RENAME COLUMN <column_name> to <new_column_name>
Consider that you already have the following
Let's change the newly added column name from
email_id column to email in the
ALTER TABLE employee RENAME email_id TO email;
email_id column is renamed, as shown below.
If you try to rename column which does not exists then PostgreSQL will raise error.
Rename Multiple Columns
Postgres does not support renaming multiple columns using one statement. To rename multiple columns you need to execute ALTER TABLE RENAME COLUMN statement multiple times for each column.
ALTER TABLE employee RENAME email_id TO email; ALTER TABLE employee RENAME birthdate TO date_of_birth;
Note: If you rename column which is being referenced in another database objects like views, foregin key constraints, functions, procedures or triggers, then Postgres will automatically change column name in those dependent objects.
For example, if
email_id column of the
employee table is used in some database procedure and you rename
Rename Column using pgAdmin
You can change name, type, and NOT NULL constraint for one or more columns in pgAdmin by right clicking on the table name and select 'Properties'. In the popup, go to 'Columns' tab wherein you can edit the columns name, type, length, and set/unset NOT NULL constraint, etc., by either editing directly or clicking on the edit icon againts the column, as shown below.
Alternatively, you can rename a column in pgAdmin by right clicking on the column you want to rename and select 'Properties' in the context menu. This will open a popup where you can edit the column name, as shown below.
Click on the Save button to save the changes.