The PostgreSQL TRIM function returns a string with characters removed from the start, the end, or both. trim(' Ohio ') returns 'Ohio'. BTRIM, LTRIM and RTRIM do the same job.
Here’s the catch: the second argument is a set of characters, not a word. In my run, rtrim('Arctic Inc', ' Inc') returned Arcti.
The six examples cover leading zeros, character sets, tabs, join keys, CSV cleanup and NULLs. Tested on PostgreSQL 18.6 for Windows.
PostgreSQL TRIM Function Syntax
Here is the syntax:
trim ( [ LEADING | TRAILING | BOTH ] [ characters text ] FROM string text ) → text
btrim ( string text [, characters text ] ) → text
ltrim ( string text [, characters text ] ) → text
rtrim ( string text [, characters text ] ) → text
| Argument | Required | What it means |
|---|---|---|
LEADING | TRAILING | BOTH | No | Which end; ltrim = LEADING, rtrim = TRAILING, btrim = BOTH (the default). |
characters | No | Characters to strip. Default: a space. |
string | Yes | The text to clean. |
Return value: text, or NULL when any argument is NULL.
Availability: every supported release. The bytea versions of ltrim and rtrim need PostgreSQL 14+.
When to Use the TRIM Function in PostgreSQL
Use TRIM when you need to:
- clean names loaded from a CSV file or an Excel sheet
- strip leading zeros from account numbers
- match join keys that carry stray spaces
- measure real size with PostgreSQL LENGTH
Run this messy import once:
CREATE TABLE customer_import (
cust_id int PRIMARY KEY,
full_name text,
state text,
account_no text
);
INSERT INTO customer_import VALUES
(1, ' Hannah Brooks ', 'OH', '000482'),
(2, 'Tyler Reed', ' TX', '001907'),
(3, E'Grace Miller\t', 'UT ', '0000'),
(4, 'Marcus Bennett' || chr(160), E'FL\n', '073100'),
(5, NULL, 'NY', NULL);
CREATE TABLE states (code text PRIMARY KEY, state_name text);
INSERT INTO states VALUES
('OH','Ohio'), ('TX','Texas'), ('UT','Utah'), ('FL','Florida'), ('NY','New York');
Example 1: Trim Leading Zeros in PostgreSQL
Let’s start with account_no. I want it without leading zeros:
\pset null '(null)'
SELECT account_no,
trim(LEADING '0' FROM account_no) AS std_form,
ltrim(account_no, '0') AS ltrim_form
FROM customer_import
ORDER BY cust_id;
Output:
account_no | std_form | ltrim_form
------------+----------+------------
000482 | 482 | 482
001907 | 1907 | 1907
0000 | |
073100 | 73100 | 73100
(null) | (null) | (null)
(5 rows)

For 000482, both forms return 482. Watch 0000: it becomes an empty string.
To keep one zero: COALESCE(NULLIF(ltrim(account_no, '0'), ''), '0'). My PostgreSQL NULLIF tutorial explains it.
Example 2: Trim Specific Characters (a Set, Not a Substring)
Say you need to strip dollar signs and an Inc suffix:
SELECT btrim('$$19.99$$', '$') AS price,
rtrim('Arctic Inc', ' Inc') AS rtrim_set,
regexp_replace('Arctic Inc', '\s+Inc$', '') AS regex_fix;
Output:
price | rtrim_set | regex_fix
-------+-----------+-----------
19.99 | Arcti | Arctic
(1 row)

The price returns 19.99. The name returns Arcti: space, I, n and c each got stripped.
For an exact suffix, I have a separate tutorial on PostgreSQL REGEXP_REPLACE.
Example 3: Why Doesn’t PostgreSQL TRIM Remove Tabs or Newlines?
Rows 1, 3 and 4 hide spaces, a tab and chr(160). You want all three clean:
SELECT cust_id,
length(full_name) AS len,
length(trim(full_name)) AS trim_len,
length(btrim(full_name, E' \t\r\n' || chr(160))) AS fixed_len
FROM customer_import
WHERE cust_id IN (1, 3, 4);
Output:
cust_id | len | trim_len | fixed_len
---------+-----+----------+-----------
1 | 16 | 13 | 13
3 | 13 | 13 | 12
4 | 15 | 15 | 14
(3 rows)

Row 1 drops from 16 to 13, but rows 3 and 4 stay at 13 and 15 because plain trim only strips the space character. The custom set fixes both.
Trim never touches the middle of a string; PostgreSQL REPLACE does.
Example 4: Use TRIM in a JOIN or WHERE Clause
The states table has clean codes. You want every customer’s state name:
SELECT count(*) AS raw_matches
FROM customer_import c
JOIN states s ON s.code = c.state;
SELECT c.cust_id, s.state_name
FROM customer_import c
JOIN states s ON s.code = btrim(c.state, E' \t\r\n')
ORDER BY c.cust_id;
Output:
raw_matches
-------------
2
(1 row)
cust_id | state_name
---------+------------
1 | Ohio
2 | Texas
3 | Utah
4 | Florida
5 | New York
(5 rows)
The raw join matched 2 rows. Trimmed: all 5.
A plain index on state won’t help here; build one on btrim(state) with PostgreSQL CREATE INDEX.
Example 5: Trim Imported CSV Data with UPDATE
I want state fixed once:
UPDATE customer_import
SET state = btrim(state, E' \t\r\n')
WHERE state <> btrim(state, E' \t\r\n');
SELECT cust_id, '[' || state || ']' AS state
FROM customer_import
ORDER BY cust_id;
Output:
UPDATE 3
cust_id | state
---------+-------
1 | [OH]
2 | [TX]
3 | [UT]
4 | [FL]
5 | [NY]
(5 rows)

PostgreSQL reports UPDATE 3. Run the SELECT again and you should see five codes with no gaps.
The WHERE clause skips clean rows; see my PostgreSQL UPDATE tutorial.
Example 6: TRIM with char(n) and NULL
Last, the edge cases. The goal is to see char(n) and NULL:
\pset null '(null)'
SELECT length('OH '::char(4)) AS char_len,
octet_length('OH '::char(4)) AS char_bytes,
trim(NULL) AS trim_null;
Output:
char_len | char_bytes | trim_null
----------+------------+-----------
2 | 4 | (null)
(1 row)
The char(4) value has length 2 but 4 bytes. Text conversion drops it; you won’t need rtrim.
A NULL input returns NULL; PostgreSQL COALESCE supplies a default.
PostgreSQL TRIM Tips & Common Mistakes
- TRIM takes text only.
ltrim(000482, '0')failed withfunction ltrim(integer, unknown) does not exist; cast to text first. - bytea has its own forms, like
btrim(bytes, bytesremoved).
Wrapping Up
My most-used PostgreSQL TRIM call is btrim(col, E' \t\r\n'). Copy Example 1 for zeros, Example 3 for hidden whitespace, and Example 5 when a CSV import leaves padding in a column you join or filter on.
Next step: browse the complete list of PostgreSQL functions or the PostgreSQL 18 string functions page.
Two more PostgreSQL string functions worth a look:
Frequently Asked Questions
How do I remove all spaces from a string in PostgreSQL?
Use replace(col, ‘ ‘, ”); regexp_replace(col, ‘\s’, ”, ‘g’) also removes tabs.
Does SQL Server have TRIM?
Yes, since SQL Server 2017; 2022 added LEADING, TRAILING and BOTH.
How do I add leading zeros in PostgreSQL?
Use lpad(account_no, 6, ‘0’). It cuts longer strings to 6.

Kumar Saurabh is a freelance software engineer who builds web and mobile apps with Next.js, React, Flutter and Python. From 2021 to 2024, Saurabh worked as an IT analyst and developer at TSInfo Technologies, writing technical articles on SQL and PostgreSQL. Saurabh writes the PostgreSQL, MySQL, MariaDB and MongoDB tutorials on DatabaseFAQs.com and holds a Master of Computer Applications (MCA) from Chandigarh University.