Postgresql – Modify all array entries in all records of a table at once

arraypostgresqlupdate

I have a schema that involves an array of timestamps. Due to an import problem all the timestamps are off by one hour (time zone issue). I'd like to update all the timestamps in all the records with one UPDATE statement.

Best Answer

You can update all the array entries like this:

UPDATE ambient_properties ap
SET recording_time = 
    (
        SELECT ARRAY(
            SELECT unnest(ap.recording_time) - interval '1 hours' 
        )
    );

The same strategy should also work for values of other types.

SQLFiddle