Add a suffix to duplicates on PostgreSQL
You can use ROW_NUMBER function to add a suffix to each repeated value.

If you'd rather avoid the first one (mostly for those rows without duplicates):

with ct as
(
  select id , row_number() over (partition by data order by id) rn
  from   t
)
update t
set    data = data || case when ct.rn = 1 then '' else (ct.rn-1)::text end
from   ct
where  ct.id = t.id;





select * from t;



id | data
-: | :---
 1 | foo 
 2 | foo1
 3 | foo2
 4 | bar 
 5 | bar1



db&lt;>fiddle here