How Log() Works in PostgreSQL

In PostgreSQL, log() is a mathematical function that returns the base 10 logarithm of its argument.

However, it also allows you to optionally specify a base with which to use.

Syntax

This function can be used in the following ways:

log(dp or numeric)
log10(dp or numeric)
log(b numeric, x numeric)

Where dp is a double precision value.

Where log(b numeric, x numeric) returns the logarithm to base b.

Example – First Syntax

Here’s an example of how it works using the first syntax.

SELECT log(20);

Result:

1.3010299956639813

Example – Second Syntax

The previous example is the same as using the second syntax.

SELECT log10(20);

Result:

1.3010299956639813

Example – Third Syntax

Here’s an example of how it works using the third syntax.

SELECT log(10, 20);

Result:

1.3010299956639812

That example uses base 10 (because the first argument is 10).

Here’s another example that uses base 2.

SELECT log(2, 20);

Result:

4.3219280948873623

Fractions

This example includes a fractional part in the argument.

SELECT log(20.35);

Result:

1.3085644135612388

Expressions

You can include expressions, such as this.

SELECT log(20 * 2);

Result:

1.6020599913279623