PostgreSQL Functions Reference

Updated · 5 min read

PostgreSQL has hundreds of built-in functions; these are the ones you'll use most. Each section lists the functions with what they do, then a query that runs every example, so the results you see are PostgreSQL's real answers. For explanations, follow the lesson links. Aggregates (count, sum, avg, min, max, string_agg) are covered in Aggregate Functions, and window functions in Window Functions.

String Functions

Lesson: String Functions.

ExampleWhat it does
lower('NaVika')Lowercase
upper('navika')Uppercase
initcap('navika anamaneni')Capitalize each word
length('Navika')Number of characters
trim(' Navika ')Remove spaces at both ends
substring('Anamaneni' FROM 1 FOR 4)Part of a string
left('Srihitha', 3)First characters
right('Srihitha', 3)Last characters
position('ika' IN 'Navika')Where a substring starts
replace('SQL Basics', 'Basics', 'Advanced')Replace text
concat('Nav', 'ika', NULL)Join, skipping NULLs
concat_ws(', ', 'Dallas', 'TX')Join with a separator
lpad('7', 3, '0')Pad on the left
split_part('2025-12-03', '-', 2)Field of a delimited string
regexp_replace('(415) 555-0142', '[^0-9]', '', 'g')Regular expression replace
reverse('abc')Reverse
SQL▶ Try it
SELECT example, result
FROM (VALUES
  (1, 'lower(''NaVika'')', (lower('NaVika'))::text),
  (2, 'upper(''navika'')', (upper('navika'))::text),
  (3, 'initcap(''navika anamaneni'')', (initcap('navika anamaneni'))::text),
  (4, 'length(''Navika'')', (length('Navika'))::text),
  (5, 'trim(''  Navika  '')', (trim('  Navika  '))::text),
  (6, 'substring(''Anamaneni'' FROM 1 FOR 4)', (substring('Anamaneni' FROM 1 FOR 4))::text),
  (7, 'left(''Srihitha'', 3)', (left('Srihitha', 3))::text),
  (8, 'right(''Srihitha'', 3)', (right('Srihitha', 3))::text),
  (9, 'position(''ika'' IN ''Navika'')', (position('ika' IN 'Navika'))::text),
  (10, 'replace(''SQL Basics'', ''Basics'', ''Advanced'')', (replace('SQL Basics', 'Basics', 'Advanced'))::text),
  (11, 'concat(''Nav'', ''ika'', NULL)', (concat('Nav', 'ika', NULL))::text),
  (12, 'concat_ws('', '', ''Dallas'', ''TX'')', (concat_ws(', ', 'Dallas', 'TX'))::text),
  (13, 'lpad(''7'', 3, ''0'')', (lpad('7', 3, '0'))::text),
  (14, 'split_part(''2025-12-03'', ''-'', 2)', (split_part('2025-12-03', '-', 2))::text),
  (15, 'regexp_replace(''(415) 555-0142'', ''[^0-9]'', '''', ''g'')', (regexp_replace('(415) 555-0142', '[^0-9]', '', 'g'))::text),
  (16, 'reverse(''abc'')', (reverse('abc'))::text)
) AS v(n, example, result)
ORDER BY n;
Output
                       example                       |      result
-----------------------------------------------------+------------------
 lower('NaVika')                                     | navika
 upper('navika')                                     | NAVIKA
 initcap('navika anamaneni')                         | Navika Anamaneni
 length('Navika')                                    | 6
 trim('  Navika  ')                                  | Navika
 substring('Anamaneni' FROM 1 FOR 4)                 | Anam
 left('Srihitha', 3)                                 | Sri
 right('Srihitha', 3)                                | tha
 position('ika' IN 'Navika')                         | 4
 replace('SQL Basics', 'Basics', 'Advanced')         | SQL Advanced
 concat('Nav', 'ika', NULL)                          | Navika
 concat_ws(', ', 'Dallas', 'TX')                     | Dallas, TX
 lpad('7', 3, '0')                                   | 007
 split_part('2025-12-03', '-', 2)                    | 12
 regexp_replace('(415) 555-0142', '[^0-9]', '', 'g') | 4155550142
 reverse('abc')                                      | cba
(16 rows)

Numeric Functions

Lesson: Numeric Functions.

ExampleWhat it does
round(123.456, 2)Round to n places
trunc(123.456, 1)Cut to n places
ceil(4.1)Round up
floor(4.9)Round down
abs(-12)Absolute value
mod(17, 5)Remainder
power(2, 10)Power
sqrt(81)Square root
greatest(3, 7, 5)Largest argument
least(3, 7, 5)Smallest argument
sign(-4)Sign: -1, 0 or 1
width_bucket(275, 0, 600, 6)Which bucket a value falls in
SQL▶ Try it
SELECT example, result
FROM (VALUES
  (1, 'round(123.456, 2)', (round(123.456, 2))::text),
  (2, 'trunc(123.456, 1)', (trunc(123.456, 1))::text),
  (3, 'ceil(4.1)', (ceil(4.1))::text),
  (4, 'floor(4.9)', (floor(4.9))::text),
  (5, 'abs(-12)', (abs(-12))::text),
  (6, 'mod(17, 5)', (mod(17, 5))::text),
  (7, 'power(2, 10)', (power(2, 10))::text),
  (8, 'sqrt(81)', (sqrt(81))::text),
  (9, 'greatest(3, 7, 5)', (greatest(3, 7, 5))::text),
  (10, 'least(3, 7, 5)', (least(3, 7, 5))::text),
  (11, 'sign(-4)', (sign(-4))::text),
  (12, 'width_bucket(275, 0, 600, 6)', (width_bucket(275, 0, 600, 6))::text)
) AS v(n, example, result)
ORDER BY n;
Output
           example            | result
------------------------------+--------
 round(123.456, 2)            | 123.46
 trunc(123.456, 1)            | 123.4
 ceil(4.1)                    | 5
 floor(4.9)                   | 4
 abs(-12)                     | 12
 mod(17, 5)                   | 2
 power(2, 10)                 | 1024
 sqrt(81)                     | 9
 greatest(3, 7, 5)            | 7
 least(3, 7, 5)               | 3
 sign(-4)                     | -1
 width_bucket(275, 0, 600, 6) | 3
(12 rows)

Date and Time Functions

Lesson: Date Functions.

ExampleWhat it does
DATE '2025-12-03' + 30Add days
DATE '2026-01-05' - DATE '2025-12-03'Days between
DATE '2025-12-03' + INTERVAL '1 month'Add an interval
EXTRACT(DOW FROM DATE '2025-12-03')Day of week, 0 = Sunday
date_trunc('month', DATE '2025-12-19')::dateStart of the month
to_char(DATE '2025-12-03', 'Mon DD, YYYY')Format a date
age(DATE '2026-01-05', DATE '2024-08-19')Years, months and days between
make_date(2025, 12, 3)Build a date
(DATE '2025-12-01' + INTERVAL '1 month - 1 day')::dateLast day of the month
to_date('12/03/2025', 'MM/DD/YYYY')Parse text as a date
SQL▶ Try it
SELECT example, result
FROM (VALUES
  (1, 'DATE ''2025-12-03'' + 30', (DATE '2025-12-03' + 30)::text),
  (2, 'DATE ''2026-01-05'' - DATE ''2025-12-03''', (DATE '2026-01-05' - DATE '2025-12-03')::text),
  (3, 'DATE ''2025-12-03'' + INTERVAL ''1 month''', (DATE '2025-12-03' + INTERVAL '1 month')::text),
  (4, 'EXTRACT(DOW FROM DATE ''2025-12-03'')', (EXTRACT(DOW FROM DATE '2025-12-03'))::text),
  (5, 'date_trunc(''month'', DATE ''2025-12-19'')::date', (date_trunc('month', DATE '2025-12-19')::date)::text),
  (6, 'to_char(DATE ''2025-12-03'', ''Mon DD, YYYY'')', (to_char(DATE '2025-12-03', 'Mon DD, YYYY'))::text),
  (7, 'age(DATE ''2026-01-05'', DATE ''2024-08-19'')', (age(DATE '2026-01-05', DATE '2024-08-19'))::text),
  (8, 'make_date(2025, 12, 3)', (make_date(2025, 12, 3))::text),
  (9, '(DATE ''2025-12-01'' + INTERVAL ''1 month - 1 day'')::date', ((DATE '2025-12-01' + INTERVAL '1 month - 1 day')::date)::text),
  (10, 'to_date(''12/03/2025'', ''MM/DD/YYYY'')', (to_date('12/03/2025', 'MM/DD/YYYY'))::text)
) AS v(n, example, result)
ORDER BY n;
Output
                        example                         |        result
--------------------------------------------------------+-----------------------
 DATE '2025-12-03' + 30                                 | 2026-01-02
 DATE '2026-01-05' - DATE '2025-12-03'                  | 33
 DATE '2025-12-03' + INTERVAL '1 month'                 | 2026-01-03 00:00:00
 EXTRACT(DOW FROM DATE '2025-12-03')                    | 3
 date_trunc('month', DATE '2025-12-19')::date           | 2025-12-01
 to_char(DATE '2025-12-03', 'Mon DD, YYYY')             | Dec 03, 2025
 age(DATE '2026-01-05', DATE '2024-08-19')              | 1 year 4 mons 17 days
 make_date(2025, 12, 3)                                 | 2025-12-03
 (DATE '2025-12-01' + INTERVAL '1 month - 1 day')::date | 2025-12-31
 to_date('12/03/2025', 'MM/DD/YYYY')                    | 2025-12-03
(10 rows)

Conversion and Conditional

Lesson: Type Conversion.

ExampleWhat it does
CAST('450.00' AS NUMERIC)Convert a type
'2025-12-03'::datePostgreSQL cast shorthand
to_char(1234.5, 'FM9,999.00')Format a number
COALESCE(NULL, 'fallback')First non-NULL
NULLIF(0, 0)NULL when equal
CASE WHEN 450 > 400 THEN 'high' ELSE 'low' ENDConditional value
pg_input_is_valid('2026-13-01', 'date')Can this text be read as the type?
SQL▶ Try it
SELECT example, result
FROM (VALUES
  (1, 'CAST(''450.00'' AS NUMERIC)', (CAST('450.00' AS NUMERIC))::text),
  (2, '''2025-12-03''::date', ('2025-12-03'::date)::text),
  (3, 'to_char(1234.5, ''FM9,999.00'')', (to_char(1234.5, 'FM9,999.00'))::text),
  (4, 'COALESCE(NULL, ''fallback'')', (COALESCE(NULL, 'fallback'))::text),
  (5, 'NULLIF(0, 0)', (NULLIF(0, 0))::text),
  (6, 'CASE WHEN 450 > 400 THEN ''high'' ELSE ''low'' END', (CASE WHEN 450 > 400 THEN 'high' ELSE 'low' END)::text),
  (7, 'pg_input_is_valid(''2026-13-01'', ''date'')', (pg_input_is_valid('2026-13-01', 'date'))::text)
) AS v(n, example, result)
ORDER BY n;
Output
                    example                     |   result
------------------------------------------------+------------
 CAST('450.00' AS NUMERIC)                      | 450.00
 '2025-12-03'::date                             | 2025-12-03
 to_char(1234.5, 'FM9,999.00')                  | 1,234.50
 COALESCE(NULL, 'fallback')                     | fallback
 NULLIF(0, 0)                                   | NULL
 CASE WHEN 450 > 400 THEN 'high' ELSE 'low' END | high
 pg_input_is_valid('2026-13-01', 'date')        | false
(7 rows)

Was this tutorial helpful?