Skip to content
Search

PostgreSQL:DataTypes

lastmod 2023-07-16created 2021-09-155 edits

Data types

Type

Description

smallint

2-byte integer

integer

4-byte integer

bigint

8-byte integer

decimal(a,a)/numeric(a,s)

Decimal type

real

6-digit single-precision floating point

double precision

15-digit double-precision floating point

serial

4-byte serial number

bigserial

8-byte serial number

date

Date

time

Time

timestamp

Date and time

char(문자수)/character

Fixed-length string (up to 4096 characters)

varchar(문자수)/charcter varying

Variable-length string (up to 4096 characters)

text

Unlimited text

Large Object

oid type

boolean/bool

true/false

JSON type

Postgresql supports the JSON data type.

  • JSON: The JSON data type was added in PostgreSQL 9.2, so JSON objects can be stored in columns. This is a very useful feature: you can take advantage of being Schemaless like NoSQL while still using the features of an RDB.
  • JSONB: The JSONB data type was added in PostgreSQL 9.4. According to the documentation, Insert is slower than JSON, but because it is stored in a binary format, processing is much more flexible.

Do not use varchar by default

Don't use the type varchar(n) by default. Consider varchar (without the length limit) or text instead.

Why not?

varchar(n) is a variable width text field that will throw an error if you try and insert a string longer than n characters (not bytes) into it.

varchar (without the (n)) or text are similar, but without the length limit. If you insert the same string into the three field types they will take up exactly the same amount of space, and you won't be able to measure any difference in performance.

If what you really need is a text field with an length limit then varchar(n) is great, but if you pick an arbitrary length and choose varchar(20) for a surname field you're risking production errors in the future when Hubert Blaine Wolfe­schlegel­stein­hausen­berger­dorff signs up for your service.

Some databases don't have a type that can hold arbitrary long text, or if they do it's not as convenient or efficient or well-supported as varchar(n). Users from those databases will often use something like varchar(255) when what they really want is text.

If you need to constrain the value in a field you probably need something more specific than a maximum length - maybe a minimum length too, or a limited set of characters - and a check constraint can do all of those things as well as a maximum string length.

When should you?

When you want to, really. If what you want is a text field that will throw an error if you insert too long a string into it, and you don't want to use an explicit check constraint then varchar(n) is a perfectly good type. Just don't use it automatically without thinking about it.

Also, the varchar type is in the SQL standard, unlike the text type, so it might be the best choice for writing super-portable applications.

Limiting character length instead of varchar(n)

See PostgreSQL:Constraints#Character length limit. In short:

ALTER TABLE names ADD CONSTRAINT namechk CHECK (char_length(name) <= 255);

See also

Favorite site

author Zer0 sitemap