Timestamptz standardization

From CarlosCaPe/octorato · View interactive version

Timestamptz standardization is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Timestamp Standardization (timestamptz)

How to use this skill

Skill file

Timestamp Standardization (timestamptz)

Source: PostgreSQL Best Practices

-- "Use timestamptz (UTC). Avoid timezone-naive types."

What

Converting all timestamp without time zone columns to timestamptz

(timestamp with time zone) and ensuring UTC semantics across the schema.

Why

timestamp without time zone stores a bare datetime with **no timezone

information**. PostgreSQL interprets it differently depending on the session's

TimeZone setting. This causes:

clients assume local time

timestamptz stores the value in UTC internally and converts for display

based on the session timezone. This eliminates all ambiguity.

Official PG 16 specification (datatype-datetime.html):

time zone -- always spell out timestamptz` explicitly

How

Step 1: Assess scope

-- Find all timestamp columns that are NOT timestamptz
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'public'
  AND data_type = 'timestamp without time zone'
ORDER BY table_name, ordinal_position;

Step 2: Convert with explicit UTC reinterpretation

ALTER TABLE public."MyTable"
    ALTER COLUMN "CreatedDate" TYPE timestamptz
    USING "CreatedDate" AT TIME ZONE 'UTC';

The AT TIME ZONE 'UTC' clause tells PostgreSQL: "these existing values

were meant to be UTC -- store them as UTC timestamptz."

Step 3: Handle dependent views

Views that SELECT timestamp columns will break if the underlying type

changes. Drop and recreate them:

-- Save view definition
SELECT pg_get_viewdef('public."MyView"'::regclass, true);

-- Drop view
DROP VIEW IF EXISTS public."MyView";

-- Alter column type
ALTER TABLE public."MyTable"
    ALTER COLUMN "CreatedDate" TYPE timestamptz
    USING "CreatedDate" AT TIME ZONE 'UTC';

-- Recreate view
CREATE VIEW public."MyView" AS ...;

Step 4: Set default for new rows

ALTER TABLE public."MyTable"
    ALTER COLUMN "CreatedDate" SET DEFAULT now();
    -- now() returns timestamptz, not timestamp

Decision Matrix

| Scenario | Use |

|----------|-----|

| Any new column storing "when something happened" | timestamptz NOT NULL DEFAULT now() |

| Columns storing a user-entered date only (no time) | date |

| Columns storing a duration or interval | interval |

| Legacy column with timestamp without time zone | Convert to timestamptz |

Key PostgreSQL Behavior

-- Session timezone affects display, NOT storage
SET timezone = 'America/Los_Angeles';
SELECT now();  -- displays in Pacific time
SELECT now() AT TIME ZONE 'UTC';  -- displays in UTC

-- timestamptz stores in UTC regardless of session
INSERT INTO t (ts) VALUES ('2026-03-04 10:00:00-05');
-- Stored as: 2026-03-04 15:00:00+00 (UTC)

When to Use

Where We Used It

Related Skills

References

-- "Schema design and data types" and Backlog #2

-- Finding #2

Gotchas

on very large tables, plan for downtime or use a staged approach

without it, PostgreSQL assumes the session timezone

differently after conversion

CURRENT_TIMESTAMP (same, but now() is more portable in PL/pgSQL)


Category: DDL | Origin: , , ,

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.