Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the maximum length of varchar(n) in postgresql 9.2 and which is best to use varchar(n) or text?

Hi I am using postgresql 9.2 and I want to use varchar(n) to store some long string but I don't know the maximum length of character which varchar(n) supports. and which one is better to use so could you please suggest me? thanks

like image 827
KRISHNA Avatar asked Apr 10 '14 06:04

KRISHNA


People also ask

What is the max length of VARCHAR in Postgres?

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

Should I use VARCHAR or text in Postgres?

Different from other database systems, in PostgreSQL, there is no performance difference among three character types. In most cases, you should use TEXT or VARCHAR . And you use the VARCHAR(n) when you want PostgreSQL to check for the length.

What is the maximum length of VARCHAR?

The size of the maximum size (m) parameter of a VARCHAR column can range from 1 to 255 bytes. If you are placing an index on a VARCHAR column, the maximum size is 254 bytes. You can store character strings that are shorter, but not longer, than the m value that you specify.

What length should I set VARCHAR?

The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.


1 Answers

tl;dr: 1 GB (each character (really: codepoint) may be represented by 1 or more bytes, depending on where they are on a unicode plane - assuming a UTF-8 encoded database). You should always use text datatype for arbitrary-length character data in Postgresql now.

Explanation: varchar(n) and text use the same backend storage type (varlena): a variable length byte array with a 32bit length counter. For indexing behavior text may even have some performance benefits. It is considered a best practice in Postgres to use text type for new development; varchar(n) remains for SQL standard support reasons. NB: varchar() (with empty brackets) is a Postgres-specific alias for text.

See also: http://www.postgresql.org/about/

like image 190
cowbert Avatar answered Oct 12 '22 16:10

cowbert