Duplicating column to same table in PostgreSQL

copy, database, postgresql

Solution

I found a relatively simple way to do this in two commands:

ALTER TABLE mytable ADD COLUMN "new_column" <DATA TYPE STUFF FROM OLD COLUMN>;
UPDATE mytable SET new_column = old_column;

Didn't realise it would be this easy. I didn't lock the table as that column isn't used too frequently so a small slowdown would be okay.

Problem

I'm looking to rename a column in PostgreSQL with no downtime. My code will depend on the column name so I'd like to duplicate the column with a new name along with the contents and data type of the existing column, then push the code changes before deleting the original column. Is there a Postgres command for duplicating a column with its contents into the same table?

Original source