PostgreSQL 18 reference · Math Functions
PostgreSQL LOG() Function
Return the base-10 logarithm of a number, or the logarithm in another base with log(b, x). log10() is a synonym.
PostgreSQL 18
Syntax
log(x) | log(b numeric, x numeric) | log10(x)
Parameters
| Parameter | Description |
|---|---|
b optional |
The base. |
x |
A number (integer, numeric or double precision). |
Return value
A number.
Examples
SELECT log(1000) AS base10, log(2, 1024) AS base2;
Output
base10 | base2
--------+---------------------
3 | 10.0000000000000000
(1 row)Run this on your computer — see “Get Started” for setup.
SELECT name, price, floor(log(price)) + 1 AS digits
FROM products
WHERE product_id IN (6, 8, 11);
Output
name | price | digits -------------+--------+-------- 4K Monitor | 329.00 | 3 Coffee Mug | 12.00 | 2 Rubber Duck | 5.50 | 1 (3 rows)
Run this on your computer — see “Get Started” for setup.
In other languages
LOG()· SQLite
Related
LN()· Return the natural logarithm (base e) of a number. exp() is the inverse.POWER()· Raise a number to a power, so power(2, 10) is 1024. pow() is another name for the same function.- SQL Aggregate Functions · tutorial