Skip to main content
Database Administration & Troubleshooting

psql Date Difference: Syntax, Examples, and Best Practices

Date Difference in psql

psql date difference is the SQL operation to compute the interval between two PostgreSQL date/timestamp values using AGE(), EXTRACT(), or direct subtraction, executed via the psql CLI.

SELECT EXTRACT(YEAR FROM AGE('2024-09-20', '2020-09-20')) AS year_difference;

Syntax

-- Using AGE() for interval
SELECT AGE(date1, date2);

-- Extracting a specific unit
SELECT EXTRACT(YEAR FROM AGE(date1, date2));
SELECT EXTRACT(MONTH FROM AGE(date1, date2));
SELECT EXTRACT(DAY FROM AGE(date1, date2));

-- Direct subtraction for days/weeks (both operands DATE)
SELECT date1::date - date2::date AS days;
SELECT (date1::date - date2::date) / 7 AS weeks;

-- Precise hour/minute/second difference using EPOCH
SELECT EXTRACT(EPOCH FROM (timestamp1 - timestamp2)) / 3600 AS hours;

Tested on PostgreSQL 15 with psql (15.4) on Ubuntu 22.04.

Functions and Options

Function Return Type Description Example
AGE(timestamp1, timestamp2) interval Exact interval between two timestamps (years, months, days, time). Order: AGE(date1, date2) = date1 – date2. AGE('2024-09-20','2020-09-20')4 years
EXTRACT(field FROM interval) numeric Extracts a single field (YEAR, MONTH, DAY, etc.) from an interval. EXTRACT(YEAR FROM AGE('2024-09-20','2020-09-20'))4
date - date integer Simple subtraction of two DATE values; returns difference in days. '2024-11-08'::date - '2024-10-25'::date14
EXTRACT(EPOCH FROM interval) numeric Total seconds in the interval; divide for hours, minutes, etc. EXTRACT(EPOCH FROM ('2024-09-20 18:00:00' - '2024-09-20 08:00:00')) / 360010
DATE_PART('field', interval) numeric Alternative to EXTRACT; identical behavior. DATE_PART('year', AGE(...))

Usage Examples

1. Years between two dates

psql -d mydb -c "SELECT EXTRACT(YEAR FROM AGE('2024-09-20', '2020-09-20')) AS year_difference;"

Returns 4. AGE() computes the full interval; EXTRACT(YEAR) isolates the year component. Ideal for age or tenure calculations.

See also  SQL Server DATEADD Function: Syntax, Parameters, and Examples

2. Days between two dates (simple subtraction)

psql -d mydb -c "SELECT ('2024-11-08'::date - '2024-10-25'::date) AS difference_in_days;"

Returns 14. Fastest method for day-level accuracy. Requires explicit ::date cast.

3. Hours between timestamps

psql -d mydb -c "SELECT EXTRACT(EPOCH FROM ('2024-09-20 18:00:00'::timestamp - '2024-09-20 08:00:00'::timestamp)) / 3600 AS hour_difference;"

Returns 10. EXTRACT(EPOCH) gives total seconds; dividing by 3600 yields hours. Useful for SLA monitoring and runtime calculations.

Frequently Asked Questions

What is the difference between using AGE() and simple subtraction of two timestamps in psql?

Answer: AGE() returns a human-readable interval with years, months, days; subtraction returns exact days or seconds. For example, AGE(timestamp '2025-03-01', timestamp '2024-01-15') yields ‘1 year 1 mon 14 days’, while timestamp '2025-03-01' - timestamp '2024-01-15' yields an interval representing 410 days. To get exact days, use EXTRACT(EPOCH FROM (end - start)) / 86400.

When should I use EXTRACT(EPOCH FROM …) for date difference?

Answer: Use EXTRACT(EPOCH FROM ...) for precise duration calculations (e.g., latency, billing) without rounding. Example: SELECT EXTRACT(EPOCH FROM (end_time - start_time)) AS seconds_diff FROM events; To convert to milliseconds, multiply by 1000.

How do I fix “ERROR: operator does not exist: timestamp without time zone – timestamp without time zone” in psql?

Answer: Explicitly cast both operands to timestamp with time zone or use explicit INTERVAL subtraction. Fix: SELECT (end_time::timestamptz - start_time::timestamptz) AS diff FROM log; Or ensure both columns are timestamptz by altering the table.

What is the fastest way to calculate the difference in days between two timestamp columns for millions of rows?

Answer: Cast to date and subtract: SELECT (end::date - start::date) AS days_exact FROM large_table; This truncates time but is fastest. For sub-second precision, subtract timestamps and extract epoch.