PostgreSQL Math Functions

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

FunctionDescriptionExampleResult
ABSCalculate the absolute value of a number.ABS(-10)10
CBRTCalculate the cube root of a number.CBRT(8)2
CEILRound a number to the nearest integer greater than or equal to a specified number.CEIL(-12.8)-12
CEILINGSame as CEIL
DEGREESConvert radians to degrees.DEGREES(PI())180
DIVReturn the integer quotient of two numeric values.DIV(8,3)2
EXPReturn the exponential of a number.EXP(1)2.71
FLOORRound a number down to the nearest integer, which is less than or equal to the number.FLOOR(10.6)10
LNReturn the natural logarithm of a numeric value.LN(3)1.0986
LOGReturn the base 10 logarithms of a numeric value.LOG(1000)3
LOGReturn the logarithm of a numeric value to a specified base.LOG(2, 64)6
MODDivide the first parameter by the second one and return the remainder.MOD(10,4)2
PIReturn the value of PI.PI()3.14159
POWERRaise a number to a specific power.POWER(5, 3)125
RADIANSConvert degrees to radiansRADIANS(180)3.14159
ROUNDRound a number to the nearest integer with the number of decimal places.ROUND(10.3)10
SCALEReturn the count of decimal digits in the fractional part of a number, which is a scale.SCALE(1.234)3
SIGNReturn -1 if the number is negative, 0 if the number is zero, and 1 if the number is positive.SIGN(-1)-1
SQRTReturn the square root of a numeric value.SQRT(3.0)1.73205
TRIM_SCALEReduce the scale of a number by removing trailing zeroes.TRIM_SCALE(12.300)12.3
TRUNCTruncate a numeric value to a whole number of the specified decimal placesTRUNC(12.3)12
WIDTH_BUCKETAssign a numeric value to a bucket in an equiwidth histogram.WIDTH_BUCKET(1,1,10,5)1
RANDOMGenerate a random number between 0 and 1RANDOM()0.9684