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
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With