Skip to main content
deep link
Source Link
Erwin Brandstetter
  • 182.1k
  • 28
  • 457
  • 620

You already saw my related answer addressing the reverse conversion timestamptimestamptz without table rewrite:

Well, since Postgres 12 the same "easter egg" feature applies to timestamptztimestamp as well. The release notesrelease notes:

Allow ALTER TABLE ... SET DATA TYPE changing between timestamp and timestamptz to avoid a table rewrite when the session time zone is UTC (Noah Misch)

In the UTC time zone, these two data types are binary compatible.

So just:

SET timezone = 'UTC';  -- unless already set to UTC
ALTER TABLE tbl ALTER COLUMN ts TYPE timestamp;
RESET timezone;        -- optional

And you don't have to mess with system catalogs directly (which should be avoided if at all possible).

You already saw my related answer addressing the reverse conversion timestamptimestamptz without table rewrite:

Well, since Postgres 12 the same "easter egg" feature applies to timestamptztimestamp as well. The release notes:

Allow ALTER TABLE ... SET DATA TYPE changing between timestamp and timestamptz to avoid a table rewrite when the session time zone is UTC (Noah Misch)

In the UTC time zone, these two data types are binary compatible.

So just:

SET timezone = 'UTC';  -- unless already set to UTC
ALTER TABLE tbl ALTER COLUMN ts TYPE timestamp;
RESET timezone;       -- optional

And you don't have to mess with system catalogs directly (which should be avoided if at all possible).

You already saw my related answer addressing the reverse conversion timestamptimestamptz without table rewrite:

Well, since Postgres 12 the same "easter egg" feature applies to timestamptztimestamp as well. The release notes:

Allow ALTER TABLE ... SET DATA TYPE changing between timestamp and timestamptz to avoid a table rewrite when the session time zone is UTC (Noah Misch)

In the UTC time zone, these two data types are binary compatible.

So just:

SET timezone = 'UTC';  -- unless already set to UTC
ALTER TABLE tbl ALTER COLUMN ts TYPE timestamp;
RESET timezone;        -- optional

And you don't have to mess with system catalogs directly (which should be avoided if at all possible).

Source Link
Erwin Brandstetter
  • 182.1k
  • 28
  • 457
  • 620

You already saw my related answer addressing the reverse conversion timestamptimestamptz without table rewrite:

Well, since Postgres 12 the same "easter egg" feature applies to timestamptztimestamp as well. The release notes:

Allow ALTER TABLE ... SET DATA TYPE changing between timestamp and timestamptz to avoid a table rewrite when the session time zone is UTC (Noah Misch)

In the UTC time zone, these two data types are binary compatible.

So just:

SET timezone = 'UTC';  -- unless already set to UTC
ALTER TABLE tbl ALTER COLUMN ts TYPE timestamp;
RESET timezone;       -- optional

And you don't have to mess with system catalogs directly (which should be avoided if at all possible).