The example in the Postgres manual (as well as the working fiddle by @mvp) transform an integer
column (representing a UNIX epoch) to timestamptz
.
The error message as well as your title clearly indicate you are trying to convert a timestamp
to timestamptz
. And this just works automatically, without explicit cast.
ALTER TABLE test ALTER created_at TYPE timestamptz;
More about timestamp vs. timestamptz:
Ignoring timezones altogether in Rails and PostgreSQL
timestamp
values are always interpreted according to the time zone setting of your session. To assume the time zone UTC for the conversion:
BEGIN;
SET LOCAL timezone='UTC';
ALTER TABLE test ALTER created_at TYPE timestamptz;
COMMIT;
Or use the AT TIME ZONE
construct:
ALTER TABLE test ALTER created_at TYPE timestamptz
USING created_at AT TIME ZONE 'UTC';
You can assume any time zone this way.