How to Rename a Column in PostgreSQL Without Disrupting Users

ddlpostgresql

You can rename a column using the following statement:

ALTER TABLE table_name RENAME COLUMN column_name TO new_column_name;

However, this brings the old column out of commission immediately. Any use sites that used the old column name will start having problems.

Is there a way to put the table into a transitory state in which both the old and new name can be used to refer to the column, so that I can move over my clients and then afterwards decommission the old name?

Best Answer

Rename the column, add a generated column with the old name. If inserts or updates of the old column haven't been corrected yet, provide insert and update triggers to handle the new column. When no logic refers to the old column anymore, drop it.