PostgreSQL rpad() Function

The PostgreSQL rpad() function right-pads the specified string with the specified characters to the specified length.

If you want to left pad a string, use the lpad() function.

rpad() Syntax

This is the syntax of the PostgreSQL rpad() function:

rpad(string, length)

or

rpad(string, length, characters)

Parameters

str

Required. The string to be padded.

length

Required. The length of the string to pad to.

characters

Optional. The character to use for padding. The default is a space.

Return value

The PostgreSQL rpad() function return the specified string str that was right-padded with the string characters (a space by default) to a length of length characters.

If length of str is greater than length, str will be truncated on the right.

If any of the arguments are NULL, the function will return NULL.

rpad() Examples

The following examples demonstrates how to right-pad a string with characters using the rpad() function.

Right pad with spaces

This example demonstrates how to right pad hello with spaces to make its length to 10.

SELECT concat(rpad('hello', 10), '|') AS "concat(rpad('hello', 10), '|')";
 concat(rpad('hello', 10), '|')
--------------------------------
 hello     |

Here, in order to make the result look more intuitive, we use the concat() function to concatenate the result and |.

You can also use the format() function to do the same thing:

SELECT format('%-10s|', 'hello') AS "format('%-10s|', 'hello')";
 format('%-10s|', 'hello')
---------------------------
 hello     |

Right pad with multiple characters

You can also right pad a string with multiple characters using the rpad() function:

SELECT rpad('Hello', 10, 'xyz') AS "rpad('Hello', 10, 'xyz')";
 rpad('Hello', 10, 'xyz')
--------------------------
 Helloxyzxy