How the Power() Function Works in PostgreSQL

In PostgreSQL, the power() function returns its first argument raised to the power of its second argument.

Syntax

The official syntax goes like this:

power(a dp, b dp)
power(a numeric, b numeric)

Where db is double precision.

Example

Here’s an example of how it works.

SELECT power(2, 3);

Result:

8

That is the equivalent of doing the following.

SELECT 2 * 2 * 2;

Result:

8

Negative Values

Here’s an example that uses negative values.

SELECT 
  power(-2, 3),
  power(2, -3),
  power(-2, -3);

Result:

 power | power | power
-------+-------+--------
    -8 | 0.125 | -0.125

Large Values

This example uses a larger number for both arguments, resulting in a very large result.

SELECT power(200, 30);

Result:

1.073741824e+69

To the Power of One

Any number raised to the power of 1 equals the number itself.

SELECT power(30, 1);

Result:

30

One To the Power of …

And 1 raised to the power of any number equals 1.

SELECT power(1, 30);

Result:

1

To the Power of Zero

Any number raised to the power of zero equals one.

SELECT power(30, 0);

Result:

1

Zero To the Power of Zero

In mathematics, zero raised to the power of zero has no agreed-upon value.

Common possibilities include 1 or leaving the expression undefined.

In Postgres, the power() function returns 1 for this expression.

SELECT power(0, 0);

Result:

1