Postgres: reduce varchar size and truncate

I currently have a Postgres 8.4 database containing a varchar (10000) column. I would like to change this to varchar (255) and truncate any data that is too long. How can i do this?

+4
source share
3 answers

Something like ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(255) USING SUBSTR(c, 1, 255)

+9
source

1) Update the column data using the substring method to truncate it

 update t set col = substring(col from 1 for 255) 

2) Then change the table column

 alter table t alter column col type varchar(255) 

Docs here http://www.postgresql.org/docs/8.4/static/sql-altertable.html

+4
source
 BEGIN; UPDATE table SET column = CAST(column as varchar(255)); ALTER TABLE table ALTER COLUMN column TYPE varchar(255); --not sure on this line. my memory is a bit sketchy COMMIT; 
0
source

Source: https://habr.com/ru/post/1304417/


All Articles