PostgreSQL query to rename and change column type with single query

In PostgreSQL, ALTER TABLE can take a series of operations. So:

ALTER TABLE <tablename> RENAME <oldcolumn> TO <newcolumn>;
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE <newtype>;

is the same as

ALTER TABLE <tablename> 
  ALTER COLUMN <columnname> TYPE <newtype>
  RENAME <oldcolumn> TO <newcolumn>;

However… why? IIRC the rename won’t cause a full-table scan, so there’s no benefit over just doing the two statements separately, within one transaction. What problem are you actually trying to solve with this?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)