PostgreSQL 18 reference · String Functions

PostgreSQL REVERSE() Function

Return a string with its characters in reverse order. Since PostgreSQL 18 it also reverses bytea values.

PostgreSQL 18 Since 9.1

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

reverse(string)

Parameters

ParameterDescription
string The input text.

Return value

Text.

Examples

Reverse text
SELECT reverse('camel') AS a, reverse('añejo') AS multibyte_safe;

Output

   a   | multibyte_safe
-------+----------------
 lemac | ojeña
(1 row)

Run this on your computer — see “Get Started” for setup.

Palindromes
SELECT word, word = reverse(word) AS palindrome
FROM (VALUES ('racecar'), ('camel')) AS t(word);

Output

  word   | palindrome
---------+------------
 racecar | t
 camel   | f
(2 rows)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 9.1Added
PostgreSQL 18Also accepts bytea, reversing the bytes.

Was this page helpful?