PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

PostgreSQL ändert Spaltendatentyp in Zeitstempel ohne Zeitzone

Wenn create_time vom Typ TEXT mit gültigem Datumswert ist, ist es einfacher, mit der Änderung wie folgt fortzufahren (es wird empfohlen, zuerst einen Tabellenspeicherauszug als Backup zu erstellen):

-- Create a temporary TIMESTAMP column
ALTER TABLE AB ADD COLUMN create_time_holder TIMESTAMP without time zone NULL;

-- Copy casted value over to the temporary column
UPDATE AB SET create_time_holder = create_time::TIMESTAMP;

-- Modify original column using the temporary column
ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING create_time_holder;

-- Drop the temporary column (after examining altered column values)
ALTER TABLE AB DROP COLUMN create_time_holder;