Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: Convert varchar to text

I screwed up and created a column as a varchar(255) where that is no longer sufficient. I've read that varchar has no performance benefits over text on Postgres, and so would like to convert the varchar to a text column in a safe way that preserves the data.

What's the best way for me to do this?

like image 577
William Jones Avatar asked May 11 '10 17:05

William Jones


People also ask

Is varchar faster than TEXT Postgres?

Both TEXT and VARCHAR have the upper limit at 1 Gb, and there is no performance difference among them (according to the PostgreSQL documentation).

What is the difference between varchar and TEXT in PostgreSQL?

Some Differences Between VARCHAR and TEXT While both data types share a maximum length of 65,535 characters, there are still a few differences: The VAR in VARCHAR means that you can set the max size to anything between 1 and 65,535. TEXT fields have a fixed max size of 65,535 characters.

How do I change the datatype in PostgreSQL?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.

What is CAST in PostgreSQL?

A cast specifies how to perform a conversion between two data types. For example, SELECT CAST(42 AS float8); converts the integer constant 42 to type float8 by invoking a previously specified function, in this case float8(int4) . (If no suitable cast has been defined, the conversion fails.)


1 Answers

ALTER TABLE table1 ALTER COLUMN column1 TYPE text; 
like image 185
rfusca Avatar answered Sep 22 '22 15:09

rfusca