PostgreSQL - EXTRACT Function - GeeksforGeeks

PostgreSQL - EXTRACT Function

Last Updated : 4 Nov, 2026

PostgreSQL EXTRACT() function retrieves a specific part of a date, time or timestamp value, such as the year, month, day, hour or minute. It is commonly used for date and time analysis in SQL queries. Supports filtering, grouping and analyzing date and time data in SQL queries.

Syntax

SELECT EXTRACT(field FROM source);

Where:

  • field: The date or time part to extract (such as YEAR, MONTH, DAY, HOUR, MINUTE or SECOND).
  • source: The date, time or timestamp value from which the field is extracted.

Examples

Consider the following events table for the examples below:

Screenshot-2026-10-05-175029

Example 1: Extract the Year from a Timestamp

Query:

SELECT event_name,
EXTRACT(YEAR FROM event_date) AS event_year
FROM events;

Output:

Screenshot-2026-10-05-175117
  • The EXTRACT() function retrieves the year from the event_data column.

Example 2: Extract the Month from a Timestamp

Query:

SELECT event_name,
EXTRACT(MONTH FROM event_date) AS event_month
FROM events;

Output:

Screenshot-2026-10-05-175202
  • The EXTRACT() function retrieves the month from the event_date column.
Comment

Explore