Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Min and Max length of a varchar in postgres?

Tags:

postgresql

Is it possible to set a minimum length for a varchar(25) field in Postgres?

So that I can prevent from accidentally adding invalid values during manual insert?

like image 267
membersound Avatar asked Feb 29 '16 12:02

membersound


People also ask

How to decide the length of the characters in PostgreSQL varchar?

We can specify the number with VARCHAR to decide the length of the characters. The number defined with the VARCHAR data type is a positive integer number. The PostgreSQL throws an error if the text’s length is greater than the length of the VARCHAR data type defined.

What is varchar data type in PostgreSQL?

It defines the varying character with length n. If the ‘n’ is not defined, then the column for which we have defined the VARCHAR data type then it will store the text string with an unlimited length of the text string with any size. How Does VARCHAR Data Type Work in PostgreSQL?

What is the size limit of various data types in PostgreSQL?

What is the size limit of various data types in postgresql? I saw somewhere that for character varying (n), varchar (n) n must be between 1 to 10485760. Is that true? What are the valid sizes for character (n), char (n) and text? Show activity on this post. The maximum size of limited character types (e.g. varchar (n)) in Postgres is 10485760.

What is the data type for characters in PostgreSQL?

Generally, for using the data type for characters, the VARCHAR is used, as it has the capability to store the values with variable length. Consider a table named TEXTS to understand the examples of the PostgreSQL VARCHAR data type.


1 Answers

You can do that using a check constraint:

alter table the_table
  add constraint check_min_length check (length(the_column) >= 10);

You probably also want to define the column as not null

like image 95
a_horse_with_no_name Avatar answered Oct 04 '22 14:10

a_horse_with_no_name