PostgreSQL Tutorial: Math Functions

July 7, 2024

This page provides you with the most commonly used PostgreSQL Math functions that help you perform various math operations quickly and effectively.

Function Description Example Result
ABS Calculate the absolute value of a number ABS(-10) 10
CBRT Calculate the cube root of a number CBRT(8) 2
CEIL Round a number up to the nearest integer, which is greater than or equal to a specified number CEIL(-12.8) -12
CEILING Same as CEIL
DEGREES Convert radians to degrees DEGREES(0.8) 45.83662361
DIV Return the integer quotient of two numeric values DIV(8,3) 2
EXP Return the exponential value in the scientific notation of a number EXP(1) 2.718281828
FLOOR Round a number down to the nearest integer, which is less than or equal to the number FLOOR(10.6) 10
LN Return the natural logarithm of a numeric value LN(3) 1.098612289
LOG Return the base 10 logarithms of a numeric value LOG(1000) 3
LOG Return the logarithm of a numeric value to a specified base LOG(2, 64) 6
MOD Divide the first parameter by the second one and return the remainder MOD(10,4) 2
PI Return the value of PI PI() 3.141592654
POWER Raise a numeric value to the power of a second numeric value POWER(5, 3) 125
RADIANS Convert degrees to radians RADIANS(60) 1.047197551
ROUND Round a number to the nearest integer or to a specified decimal places ROUND(10.3) 10
SCALE Return the number of decimal digits in the fractional part SCALE(1.234) 3
SIGN Return the sign (positive, negative) of a numeric value SIGN(-1) -1
SQRT Return the square root of a numeric value SQRT(3.0) 1.732050808
TRUNC Truncate a numeric value to a whole number of the specified decimal places TRUNC(12.3) 12
WIDTH_BUCKET Assign values to buckets in an equi-width histogram.
RANDOM Return a random number that ranges from 0 to 1 0.968435665