PostgreSQL 18 reference · String Functions

PostgreSQL REPEAT() Function

Repeat a string a given number of times. A count of 0 or less gives an empty string.

PostgreSQL 18

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

Syntax

repeat(string, number)

Parameters

ParameterDescription
string The input text.
number How many times.

Return value

Text.

Examples

Repeat text
SELECT repeat('ab', 3) AS a, repeat('=', 10) AS rule;

Output

   a    |    rule
--------+------------
 ababab | ==========
(1 row)

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

A text bar chart
SELECT name, repeat('*', (stock / 20)::int) AS stock_bar
FROM products
WHERE category_id = 2;

Output

        name         | stock_bar
---------------------+-----------
 Mechanical Keyboard | *
 Wireless Mouse      | ***
 4K Monitor          |
 USB-C Hub           | *
(4 rows)

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

Was this page helpful?