PostgreSQL function reference

A short guide to each function: what it does, how to call it, examples you can edit and run right here, and what to watch out for so you don't trip up.

These are the core functions — enough for most queries. For everything else, see the official PostgreSQL documentation(opens in a new tab).

Conditional

Numbers

Strings

LENGTH

Counts how many characters a string has.

LENGTH(<text>)
LOWER

Converts text to lowercase.

LOWER(<text>)
UPPER

Converts text to uppercase.

UPPER(<text>)
INITCAP

Capitalises the first letter of every word and makes the rest lowercase.

INITCAP(<text>)
TRIM

Removes spaces (or other characters) at the start and end of a string.

TRIM(<text>)
SUBSTRING

Cuts out part of a string: from the position you need and of the length you need.

SUBSTRING(<text> FROM <start>)
LEFT

Takes the given number of characters from the start of a string.

LEFT(<text>, <count>)
RIGHT

Takes the given number of characters from the end of a string.

RIGHT(<text>, <count>)
POSITION

Finds the character where a substring starts within a string.

POSITION(<substring> IN <text>)
STARTS_WITH

Checks whether a string starts with the given prefix.

STARTS_WITH(<text>, <prefix>)
REPLACE

Replaces every occurrence of one piece of text in a string with another.

REPLACE(<text>, <from>, <to>)
TRANSLATE

Replaces individual characters with others — each with its own, in a single pass.

TRANSLATE(<text>, <characters>, <replacements>)
REGEXP_LIKE

Checks whether a string matches a regular expression.

REGEXP_LIKE(<text>, '<pattern>')
REGEXP_MATCHES

Extracts the parts of a string that match a regular expression.

REGEXP_MATCHES(<text>, '<pattern>')
REGEXP_SUBSTR

Returns the first fragment of a string that matches a regular expression — as plain text.

REGEXP_SUBSTR(<text>, '<pattern>')
REGEXP_REPLACE

Replaces the parts of a string that match a regular expression.

REGEXP_REPLACE(<text>, '<pattern>', <to>)
SPLIT_PART

Splits a string by a separator and returns the part with the number you need.

SPLIT_PART(<text>, <separator>, <part number>)
CONCAT

Joins several values into one string, skipping NULL.

CONCAT(<value1>, <value2>, …)
FORMAT

Builds a string from a template, substituting values for %s.

FORMAT('<template>', <value1>, <value2>, …)
LPAD

Pads a string on the left to the length you need.

LPAD(<text>, <length>)
RPAD

Pads a string on the right to the length you need.

RPAD(<text>, <length>)
REPEAT

Repeats a string the given number of times.

REPEAT(<text>, <times>)
REVERSE

Reverses a string.

REVERSE(<text>)
MD5

Calculates the MD5 hash of a string — 32 hexadecimal characters.

MD5(<text>)

Dates and times

Types

Arrays and series

JSON

Full-text search

Aggregate

Window

ROW_NUMBER

Numbers rows 1, 2, 3… in a given order — across the whole result or within each group.

ROW_NUMBER() OVER (ORDER BY <sort key>)
RANK

Assigns places with ties: equal values share a place, and the next one is skipped.

RANK() OVER (ORDER BY <sort key>)
DENSE_RANK

Assigns places with ties, but without gaps: 1, 2, 2, 3.

DENSE_RANK() OVER (ORDER BY <sort key>)
PERCENT_RANK

Shows a row's relative position from 0 to 1: what share of the other rows comes before it.

PERCENT_RANK() OVER (ORDER BY <sort key>)
CUME_DIST

Shows the share of rows that come before the current one or tie with it — from 0 to 1.

CUME_DIST() OVER (ORDER BY <sort key>)
NTILE

Splits rows into the given number of groups of roughly equal size and numbers the groups.

NTILE(<group count>) OVER (ORDER BY <sort key>)
LAG

Takes a value from the previous row to compare with the current one.

LAG(<expression>) OVER (ORDER BY <sort key>)
LEAD

Takes a value from the next row to compare with the current one.

LEAD(<expression>) OVER (ORDER BY <sort key>)
FIRST_VALUE

Takes the value from the first row of the window.

FIRST_VALUE(<expression>) OVER (ORDER BY <sort key>)
LAST_VALUE

Takes the value from the last row of the window — but only with the right frame.

LAST_VALUE(<expression>) OVER ( ORDER BY <sort key> ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING )
NTH_VALUE

Takes the value from the Nth row of the window: the second, the third…

NTH_VALUE(<expression>, <n>) OVER ( ORDER BY <sort key> ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING )