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'::date → 14 |
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')) / 3600 → 10 |
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.
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.
Does psql date difference syntax work identically on AWS RDS PostgreSQL and GCP Cloud SQL PostgreSQL?
Answer: Yes, the core functions (AGE, subtraction, EXTRACT) are standard PostgreSQL and behave identically on all managed platforms. Always check the minor version for potential optimizer changes.
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.

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.