Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing the size of character varying type in postgres without data loss

Tags:

postgresql

I need to increase the size of a character varying(60) field in a postgres database table without data loss.

I have this command

alter table client_details alter column name set character varying(200); 

will this command increase the the field size from 60 to 200 without data loss?

like image 248
Elitmiar Avatar asked Mar 30 '11 15:03

Elitmiar


People also ask

What is maximum length of character varying Postgres?

The maximum limit of size character using character varying data type in PostgreSQL is 10485760. The below example shows that the size of the character using character varying data type in PostgreSQL is 10485760.

What is character varying data type in PostgreSQL?

PostgreSQL supports a character data type called VARCHAR. This data type is used to store characters of limited length. It is represented as varchar(n) in PostgreSQL, where n represents the limit of the length of the characters. If n is not specified it defaults to varchar which has unlimited length.

What is the difference between varchar and character varying in PostgreSQL?

VARCHAR is an alias for CHARACTER VARYING , so no difference, see documentation :) The notations varchar(n) and char(n) are aliases for character varying(n) and character(n) , respectively. character without length specifier is equivalent to character(1) .

What is maximum size of text data type in Postgres?

What is PostgreSQL Text datatype? In PostgreSQL, the text data type is used to keep the character of infinite length. And the text data type can hold a string with a maximum length of 65,535 bytes.


2 Answers

The correct query to change the data type limit of the particular column:

ALTER TABLE client_details ALTER COLUMN name TYPE character varying(200); 
like image 90
Peter prabu Avatar answered Sep 17 '22 17:09

Peter prabu


Referring to this documentation, there would be no data loss, alter column only casts old data to new data so a cast between character data should be fine. But I don't think your syntax is correct, see the documentation I mentioned earlier. I think you should be using this syntax :

ALTER [ COLUMN ] column TYPE type [ USING expression ]

And as a note, wouldn't it be easier to just create a table, populate it and test :)

like image 39
Erkan Haspulat Avatar answered Sep 18 '22 17:09

Erkan Haspulat