PostgreSQL TIMESTAMPTZ and EXTRACT(EPOCH) Guide
Calling EXTRACT(EPOCH FROM timestamp_column) on a PostgreSQL column without timezone awareness quietly assumes your database session timezone. If an analyst connects from Berlin while your app runs in UTC, the same SQL query outputs two different Unix integers for the exact same row.
How TIMESTAMPTZ Actually Works in PostgreSQL
Despite its name ("timestamp with time zone"), TIMESTAMPTZ does not save a timezone name inside the row. Both TIMESTAMP and TIMESTAMPTZ use 8 bytes with 1-microsecond precision and support dates from 4713 BC to 294276 AD, meaning PostgreSQL has no Year 2038 limit.
The key difference is conversion behavior. TIMESTAMPTZ translates every incoming offset to UTC when writing to disk, then formats the value into the caller's session timezone when reading. The PostgreSQL Date/Time Types manual recommends TIMESTAMPTZ for almost all production tables.
Convert Unix Timestamps Into PostgreSQL TIMESTAMPTZ
Use the built-in to_timestamp() function for Unix seconds. If your API provides 13-digit milliseconds, divide by 1000.0 so PostgreSQL keeps fractional precision.
-- Convert 10-digit Unix seconds
SELECT to_timestamp(1764892800) AT TIME ZONE 'UTC';
-- Convert 13-digit milliseconds cleanly
SELECT to_timestamp(1764892800500 / 1000.0);
Extract Unix Seconds and Milliseconds From Rows
Use EXTRACT(EPOCH FROM ...) on a TIMESTAMPTZ column to get Unix seconds as a numeric value with microsecond decimals.
-- Whole Unix seconds
SELECT FLOOR(EXTRACT(EPOCH FROM created_at))::BIGINT AS unix_sec
FROM orders;
-- Unix milliseconds (13 digits)
SELECT FLOOR(EXTRACT(EPOCH FROM created_at) * 1000)::BIGINT AS unix_ms
FROM orders;
Indexing Performance Tip for Epoch Queries
Never wrap an indexed column inside EXTRACT(EPOCH FROM created_at) > 1764892800 in your WHERE clause, because applying a function to the column disables B-tree index usage. Convert the constant parameter instead:
-- Fast: uses the B-tree index on created_at
SELECT * FROM orders
WHERE created_at >= to_timestamp(1764892800);