Sling Academy
Home/PostgreSQL/Understanding the AGE Function in PostgreSQL

Understanding the AGE Function in PostgreSQL

Last updated: January 05, 2024

Overview

PostgreSQL, often simply Postgres, is a powerful, open-source object-relational database system that uses and extends the SQL language. It provides numerous functions for developers and database administrators to work efficiently with dates and times. One such function is the AGE function, which offers a convenient way to calculate intervals between timestamps.

Background

The AGE function was introduced in PostgreSQL as part of its versatile set of date/time functions. It has been a feature of PostgreSQL for many versions, its purpose is invaluable in calculating age or duration between two timestamps.

Use Cases

The primary purpose of the AGE function is to calculate the difference between two points in time and return the result as an interval. It can be beneficial when you need to determine how much time has elapsed between two dates or simply calculate someone or something’s age given a birthdate and the current date.

Syntax and Parameters

Syntax:

AGE(timestamp1, timestamp2)

Parameters:

  • timestamp1: The later point in time. If not specified, defaults to the current date and time.
  • timestamp2: The earlier point in time. Returned Value: An interval showing the difference between timestamp1 and timestamp2.

Practical Examples

Example 1: Basic Age Calculation

Calculate the age of a person given their birthdate.

Using AGE to figure out the exact age from a birthdate.

SELECT AGE(TIMESTAMP '2000-01-01'); -- Assumes current date as the first parameter

Example 2: Duration Between Dates

Calculate the duration between two specific dates.

This example shows how to calculate the duration of a past event from today:

SELECT AGE(NOW(), TIMESTAMP '1969-07-20 20:18:04+00'); -- The exact moment of the first moon landing

Example 3: Age at Event

Determine how old a person was on a certain event date.

Often useful for historical records or to establish context, here we calculate someone’s age on a significant past date:

SELECT AGE(TIMESTAMP '2001-09-11', TIMESTAMP '2000-01-01');

Example 4: Interval Formatting

Format interval output for clarity.

Sometimes it is more useful to display the age result in years, months and days instead of the standard interval format:

SELECT EXTRACT(YEAR FROM AGE(TIMESTAMP '2000-01-01')) AS years,
       EXTRACT(MONTH FROM AGE(TIMESTAMP '2000-01-01')) AS months,
       EXTRACT(DAY FROM AGE(TIMESTAMP '2000-01-01')) AS days;

Conclusion

The AGE function in PostgreSQL is an essential tool when working with dates and times. It simplifies the process of interval calculation between timestamps, which is indispensable in many real-world applications like calculating ages, durations, and periods of time. Understanding and utilizing the AGE function can help keep your queries concise and efficient. With the provided examples, you should have a solid foundation for incorporating this function into your PostgreSQL toolbox.

Next Article: PostgreSQL: Using CURRENT_DATE and CURRENT_TIME Functions

Previous Article: Using OVERLAPS operator with dates in PostgreSQL

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB