PostgreSQL character varying length limit
I am using character varying开发者_Python百科 data type in PostgreSQL. I was not able to find this information in PostgreSQL manual. What is max limit of characters in character varying data type?
Referring to the documentation, there is no explicit limit given for the varchar(n)
type definition. But:
...
In any case, the longest possible character string that can be stored is about 1 GB. (The maximum value that will be allowed forn
in the data type declaration is less than that. It wouldn't be very useful to change this because with multibyte character encodings the number of characters and bytes can be quite different anyway. If you desire to store long strings with no specific upper limit, use text or character varying without a length specifier, rather than making up an arbitrary length limit.)
Also note this:
Tip: There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.
From documentation:
In any case, the longest possible character string that can be stored is about 1 GB.
character type in postgresql
- character varying(n), varchar(n) = variable-length with limit
- character(n), char(n) = fixed-length, blank padded
- text = variable unlimited length
based on your problem I suggest you to use type text. the type does not require character length.
In addition, PostgreSQL provides the text type, which stores strings of any length. Although the type text is not in the SQL standard, several other SQL database management systems have it as well.
source : https://www.postgresql.org/docs/9.6/static/datatype-character.html
The maximum string size is about 1 GB. Per the postgres docs:
Very long values are also stored in background tables so that they do not interfere with rapid access to shorter column values. In any case, the longest possible character string that can be stored is about 1 GB. (The maximum value that will be allowed for n in the data type declaration is less than that.)
Note that the max n you can specify for varchar is less than the max storage size. While this limit may vary, a quick check reveals that the limit on postgres 11.2 is 10 MB:
psql (11.2)
=> create table varchar_test (name varchar(1073741824));
ERROR: length for type varchar cannot exceed 10485760
Practically speaking, when you do not have a well rationalized length limit, it's suggested that you simply use varchar
without specifying one. Per the official docs,
If you desire to store long strings with no specific upper limit, use text or character varying without a length specifier, rather than making up an arbitrary length limit.
精彩评论