EXTRACT epoch Bridges Unix Time
EXTRACT(EPOCH FROM ...) renders timestamps as seconds since 1970 and to_timestamp converts back: naive values count literally, timestamptz counts from UTC.
Code
-- Unix 纪元秒与 timestamp/timestamptz 互转
SELECT
EXTRACT(EPOCH FROM TIMESTAMP '2026-01-01 00:00:00') AS epoch_from_naive,
EXTRACT(EPOCH FROM TIMESTAMPTZ '2026-01-01 00:00:00+00') AS epoch_from_utc,
EXTRACT(EPOCH FROM NOW()) AS right_now;
-- 反向:to_timestamp(纪元秒) 返回 timestamptz
SELECT to_timestamp(1767225600) AS from_epoch; -- 2026-01-01 00:00:00+00
-- naive 时间戳的 epoch 按字面值计秒,不吸收时区
SET TimeZone = 'UTC';
SELECT to_timestamp(1767225600)::timestamp AS wall_clock_utc; -- 2026-01-01 00:00:00
-- 毫秒级时间戳(常见于 JS/API)先除以 1000
SELECT to_timestamp(1767225600000 / 1000.0) AS from_millis;Inside the snippet
What runs here is SELECT — timestamp, timestamptz and now are the objects involved.
epoch_from_naive and epoch_from_utc run the same EXTRACT(EPOCH FROM …) against a naive TIMESTAMP and an offset-carrying TIMESTAMPTZ, so the pair contrasts how the two types enter the conversion; right_now reads the session clock at run time.
The to_timestamp(1767225600) statements go the opposite way, casting the epoch value back — the ::timestamp cast drops the trailing offset.
Putting it to use
The timestamp table is the working surface here.
Lines that do the work
SELECT
EXTRACT(EPOCH FROM TIMESTAMP '2026-01-01 00:00:00') AS epoch_from_naive,
EXTRACT(EPOCH FROM TIMESTAMPTZ '2026-01-01 00:00:00+00') AS epoch_from_utc,
EXTRACT(EPOCH FROM NOW()) AS right_now;
SELECT to_timestamp(1767225600)::timestamp AS wall_clock_utc; -- 2026-01-01 00:00:00
SELECT to_timestamp(1767225600) AS from_epoch; -- 2026-01-01 00:00:00+00
Interactive Tools for This Topic
Unix Timestamp ConverterConvert between Unix timestamps and human-readable dates in both directions. Supports seconds and milliseconds, with UTC and local timezone display options.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.
FAQ
- Which tables or CTEs does it reference?
- `timestamp`, `timestamptz`, `now`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.