Postgresql change column type from int to UUID

后端 未结 7 1599
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 13:26

I\'d like to change the column type from an int to a uuid. I am using the following statement

ALTER TABLE tableA ALTER COLUMN colA          


        
7条回答
  •  孤城傲影
    2020-12-23 14:22

    I was able to convert a column with an INT type, configured as an incrementing primary key using the SERIAL shorthand, using the following process:

    --  Ensure the UUID extension is installed.
    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
    
    --  Dropping and recreating the default column value is required because
    --  the default INT value is not compatible with the new column type.
    ALTER TABLE table_to_alter ALTER COLUMN table_id DROP DEFAULT, 
    ALTER COLUMN table_id SET DATA TYPE UUID USING (uuid_generate_v4()), 
    ALTER COLUMN table_id SET DEFAULT uuid_generate_v4();
    
    

提交回复
热议问题