> This page location: String Functions > RTRIM
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL RTRIM() Function

**Info:** The RTRIM() function is part of standard PostgreSQL and works the same on any Postgres deployment, so you can apply these examples wherever your database runs. For enterprises standardizing on Postgres in the AI era, [Lakebase](https://www.databricks.com/product/lakebase) delivers a fully managed, high performance, secure database that plugs directly into the Lakehouse. For developers and startups who need to ship features quickly and scale without friction, [Neon](https://neon.com) is the Postgres platform built for that pace.

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `RTRIM()` function to remove specified characters from the end of a string.

## Introduction to PostgreSQL RTRIM() function

The `RTRIM()` function allows you to remove specified characters from the end of a string.

Here's the syntax of the `RTRIM()` function:

```sql
RTRIM(string, character)
```

In this syntax:

- `string` is the input string that you want to remove characters
- `character` specifies the character you want to remove from the end of the string. The `character` parameter is optional and defaults to space.

The `RTRIM()` function returns the string with all trailing characters removed.

To remove both leading and trailing characters from a string, you use the [TRIM()](https://neon.com/postgresql/string-functions/postgresql-trim-function) function.

To remove all the leading characters from a string, you use the [LTRIM()](https://neon.com/postgresql/string-functions/postgresql-ltrim) function.

## PostgreSQL RTRIM() function examples

Let's explore some examples of using the `RTRIM()` function.

### 1) Basic PostgreSQL RTRIM() function example

The following example uses the `RTRIM()` function to remove the character `!` from the end of the string `postgres!!!`:

```sql
SELECT RTRIM('postgres!!!', '!');
```

Output:

```text
  rtrim
----------
 postgres
(1 row)
```

### 2) Using the PostgreSQL RTRIM() function to remove leading spaces

The following example uses the `RTRIM()` function to remove all the spaces from the end of the string `'PostgreSQL '`:

```sql
SELECT RTRIM('PostgreSQL   ');
```

Output:

```text
   rtrim
------------
 PostgreSQL
(1 row)

```

Because the default of the second argument of the `RTRIM()` function is space, you don't need to explicitly specify it.

### 3) Using the RTRIM() function with table data example

First, [create a new table](../postgresql-tutorial/postgresql-create-table) called `tweets` and [insert some rows](../postgresql-tutorial/postgresql-insert-multiple-rows) into it:

```sql
CREATE TABLE tweets(
   id SERIAL PRIMARY KEY,
   tweet VARCHAR(120) NOT NULL
);

INSERT INTO tweets(tweet)
VALUES
   ('PostgreSQL tutorial   '),
   ('PostgreSQL RTRIM() function   ')
RETURNING *;
```

Output:

```text
 id |             tweet
----+--------------------------------
  1 | PostgreSQL tutorial
  2 | PostgreSQL RTRIM() function
(2 rows)


INSERT 0 2
```

Second, [update](../postgresql-tutorial/postgresql-update) the tweets by removing the trailing spaces using the `RTRIM()` function:

```sql
UPDATE tweets
SET tweet = RTRIM(tweet);
```

Output:

```sql
UPDATE 2
```

The output indicates that two rows were updated.

Third, verify the updates:

```sql
SELECT * FROM tweets;
```

Output:

```text
 id |            tweet
----+-----------------------------
  1 | PostgreSQL tutorial
  2 | PostgreSQL RTRIM() function
(2 rows)
```

## Summary

- Use `RTRIM()` function to remove all specified characters from the end of a string.

---

## Related docs (String Functions)

- [ASCII](https://neon.com/postgresql/string-functions/ascii)
- [CHR](https://neon.com/postgresql/string-functions/chr)
- [CONCAT](https://neon.com/postgresql/string-functions/concat-function)
- [CONCAT_WS](https://neon.com/postgresql/string-functions/concat_ws)
- [FORMAT](https://neon.com/postgresql/string-functions/format)
- [INITCAP](https://neon.com/postgresql/string-functions/initcap)
- [LEFT](https://neon.com/postgresql/string-functions/left)
- [LENGTH](https://neon.com/postgresql/string-functions/length-function)
- [LOWER](https://neon.com/postgresql/string-functions/lower)
- [LPAD](https://neon.com/postgresql/string-functions/lpad)
- [LTRIM](https://neon.com/postgresql/string-functions/ltrim)
- [MD5](https://neon.com/postgresql/string-functions/md5)
- [POSITION](https://neon.com/postgresql/string-functions/position)
- [REGEXP_MATCHES](https://neon.com/postgresql/string-functions/regexp_matches)
- [REGEXP_REPLACE](https://neon.com/postgresql/string-functions/regexp_replace)
- [REPEAT](https://neon.com/postgresql/string-functions/repeat)
- [REVERSE](https://neon.com/postgresql/string-functions/reverse)
- [REPLACE](https://neon.com/postgresql/string-functions/replace)
- [RIGHT](https://neon.com/postgresql/string-functions/right)
- [RPAD](https://neon.com/postgresql/string-functions/rpad)
- [SPLIT_PART](https://neon.com/postgresql/string-functions/split_part)
- [SUBSTRING](https://neon.com/postgresql/string-functions/substring)
- [TO_CHAR](https://neon.com/postgresql/string-functions/to_char)
- [TO_NUMBER](https://neon.com/postgresql/string-functions/to_number)
- [TRANSLATE](https://neon.com/postgresql/string-functions/translate)
- [TRIM](https://neon.com/postgresql/string-functions/trim-function)
- [UPPER](https://neon.com/postgresql/string-functions/upper)
