PostgreSQL POWER() Function: A How-To Guide
In this article
To use the PostgreSQL power function, call POWER(base, exponent) (or the equivalent POW(base, exponent)) in any SQL expression. It returns base raised to the power of exponent, so SELECT POWER(2, 3); returns 8. That is the whole answer for the common case; the rest of this guide covers the data types, edge cases and mistakes that decide whether your query returns the number you expect.
What You'll Need
- A PostgreSQL server you can connect to, with a user that can run
SELECTstatements. - A client:
psql, pgAdmin, DBeaver, or any SQL editor that can send queries. - Basic familiarity with SQL expressions and
SELECT. - No extensions are required;
POWERandPOWare built-in.
Step-by-Step
- Connect to the database. Open your client and connect to the target database. In
psqlthat is typicallypsql -h host -U user -d dbname; in a GUI client, use the connection profile you already have. If you are unsure which database you are in, runSELECT current_database();. - Run the simplest form. Execute
SELECT POWER(2, 3);. The result column is namedpowerand contains8. You can alias it:SELECT POWER(2, 3) AS result;. - Use
POWif you prefer.SELECT POW(2, 3);returns the same value.POWis an alias forPOWERand behaves identically, so pick one and stay consistent in your codebase. - Understand the return type. With two integer arguments, PostgreSQL resolves the call to the
double precisionversion of the function, so the result is a floating-point number. If you need a numeric result with exact decimal behaviour, cast the inputs:SELECT POWER(2::numeric, 3::numeric);returns8asnumeric. This matters for money and other exact calculations. - Apply it to table columns. Suppose a table
measurementshas columnsvalueandexponent. RunSELECT value, exponent, POWER(value, exponent) AS raised FROM measurements;. The function is evaluated row by row, so each row gets its own result. - Handle fractional exponents.
POWER(9, 0.5)returns3, because a fractional exponent is a root. Negative bases with fractional exponents are not real numbers, soPOWER(-9, 0.5)raises an error (a negative number raised to a non-integer power yields a complex result). Decide in advance whether your data can contain that combination. - Know the zero and negative rules.
POWER(0, 0)returns1in PostgreSQL. A negative exponent returns the reciprocal:POWER(2, -2)returns0.25. Zero to a negative power raises a division-by-zero error, so guard against it if the exponent comes from user input or a column. - Guard against overflow and special values. Very large results can exceed the range of
double precisionand returnInfinityrather than an error. If you need to detect that, compare with'Infinity'::float8or useisfinite()on the result. If exactness matters, usenumericinputs, which raise an error on overflow instead of silently returning infinity. - Use it in
WHEREandORDER BY. You can filter on the computed value:SELECT * FROM measurements WHERE POWER(value, 2) > 100;. For readability and to avoid recomputing, put the expression in a subquery or aLATERALjoin and filter on the alias. Note that a function on a column generally prevents use of a plain index on that column. - Test the edge cases before shipping. Run a small set of queries covering integer, numeric, fractional, negative and zero inputs, and confirm the types and values match what your application expects. This is faster than debugging a production report later.
Common Mistakes
- Expecting an integer result.
POWER(2, 3)returns a floating-point value, not an integer. If you need an integer, cast explicitly, for examplePOWER(2, 3)::integer, and be aware that the cast truncates rather than rounds. - Using
^instead ofPOWER. In PostgreSQL,^is the exponentiation operator and does work, but it is easy to confuse with XOR in other languages.POWERis clearer and portable. - Forgetting operator precedence.
-2^2is parsed as-(2^2)and returns-4, not4. Use parentheses:(-2)^2. - Mixing numeric and double precision carelessly. A
numericinput produces anumericresult; adouble precisioninput produces a floating-point result with rounding. Choose deliberately, especially for financial data. - Assuming a negative base with a fractional exponent works. It raises an error rather than returning a complex number. Filter or transform such rows first.
- Ignoring
NULL. If either argument isNULL, the result isNULL. UseCOALESCEif you need a default.
Tips
- Use
POWER(x, 2)for squares andPOWER(x, 0.5)for square roots, but prefer the dedicatedSQRT(x)function for square roots because it is clearer and avoids floating-point surprises. - For cube roots and other roots, fractional exponents are fine:
POWER(27, 1.0/3)returns approximately3. Write the fraction with a decimal point (1.0/3) so integer division does not turn it into zero. - When the exponent is a column, cast it to
double precisionornumericexplicitly to avoid ambiguous function resolution. - If you compute the same power repeatedly in a query, wrap it in a subquery or a
LATERALjoin so the database evaluates it once per row rather than once per reference. - For scientific notation output, cast to
textor useto_char(); the default display of very large or very small floating-point numbers can be hard to read.
Troubleshooting
- Error: function power(integer, integer) does not exist. This usually means the arguments are of a type PostgreSQL cannot resolve, or you are calling with an unusual combination. Cast one argument explicitly, for example
POWER(value::double precision, exponent::double precision). - Error: a negative number raised to a non-integer power yields a complex result. Check the base column for negative values and either filter them out or take the absolute value first, depending on what the calculation means.
- Error: division by zero. This happens with
POWER(0, negative_exponent). Guard the exponent with aCASEexpression or exclude zero bases. - Result is
Infinity. The result exceeded the floating-point range. Switch tonumericinputs for exact arithmetic, or reduce the magnitude of the inputs. - Result is
NULL. One of the arguments isNULL. Check withIS NULLand decide whether to filter or substitute a value. - Unexpected rounding. Floating-point arithmetic is approximate. If exact decimal results are required, cast inputs to
numeric.
Frequently Asked Questions
What is the difference between POWER and POW in PostgreSQL?
They are aliases for the same function and produce identical results. Use whichever reads better in your query.
Does POWER work with numeric and double precision?
Yes. PostgreSQL provides overloads for double precision and numeric (and related types). The result type follows the argument types, so cast deliberately when exactness matters.
Can I use POWER with a column as the exponent?
Yes. Both arguments can be expressions, including column references. The function is evaluated for each row.
Why does POWER(2, 3) return 8.0 instead of 8?
With integer arguments, PostgreSQL resolves to the double precision version, so the result is displayed as a floating-point number. Cast to integer or numeric if you need a different display or type.
What does POWER(0, 0) return?
It returns 1, following the usual convention for exponentiation.
Can POWER return a complex number?
No. PostgreSQL raises an error for a negative base with a non-integer exponent rather than returning a complex result.