Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: Reduce varchar size and truncate

I currently have a Postgres 8.4 database that contains a varchar(10000) column. I'd like to change this into a varchar(255) and truncate any data that happens to be too long. How can I do this?

like image 398
William Jones Avatar asked Mar 17 '10 22:03

William Jones


2 Answers

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

like image 54
mkj Avatar answered Sep 23 '22 20:09

mkj


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

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

2) Then alter 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

like image 40
codenheim Avatar answered Sep 24 '22 20:09

codenheim