Data Types in PostgreSQL (Data Types)

Tram Ho

After understanding what PostgreSQL is and installing it, in this article I will introduce you to the data types in PostgresSQL.

Numeric Types

The numeric data types in postgresSQL have the following types, see the table below:

TypeSizeAbout
smallint2 bytes-32768 to +32768
integer4 bytes-2147483648 to +2147483648
bigint8 bytes-9223372036854775808 to +9223372036854775808
decimalvariablethis is a dynamic data type so there can be up to 131072 digits before the decimal point and 16383 digits after the decimal point
numericvariablethis is a dynamic data type so there can be up to 131072 digits before the decimal point and 16383 digits after the decimal point
real4 bytesreal numeric data type, with precision up to 6 decimal places
double precision8 bytesreal numeric data type, with precision up to 15 decimal places
smallserial2 bytes1 to 32767
serial4 bytes1 to 2147483647
bigserial8 bytes1 to 9223372036854775807

Umm, is there anything to note here, you guys are no strangers to the above data types, right? Here I will have a little note between some of the above types.

Type numeric, with this guy when using you will have 3 ways to use as follows:

NUMERIC( precision , scale )

NUMERIC( precision )

NUMERIC

In the above declaration, we will have 2 parameters, precision (maximum precision) and scale (maximum scale), as simple as this. For example, we have the number 374.2341 so according to this number, precision=7 and scale=4 and in integers then of course scale will be 0.

Next is the difference between numeric and double precision:

In this example, I will use the following command:

SELECT x, round(x::numeric) AS num_round, round(x::double precision) AS dbl_round FROM generate_series(-3.5, 3.5, 1) as x;

Let me explain a bit the above query for those of you who are not clear: we will use the generate_series function, this function will pass in 3 parameters respectively: the starting number, the ending number, and the unit. The above statement will generate numbers from -3.5 to 3.5 with each number separated by 1 unit, the round function I will use to round, before rounding, I will use the :: syntax as above to force the type to return. 2 types above, from which we can compare the difference between the two data types above.

image.png

As you can see from the above results, the two data types numeric and double precision have a difference when rounding right. According to the above results, the numeric guy will round according to the principle that if the decimal number at the position to be rounded >=0.5 will be rounded up and <0.5 will be rounded down, and for the double precision guy, it will follow the principle of rounding to the nearest even number. For example 3.5, the double precision guy will round up to 4 because 4 will be closer to 3.5 than 2 and with 2.5 it will round down to 2 because 2 will be closer to 2.5 than 4. With the above example, you should note when using the above data types, to avoid data errors.

I’ll just come here today, I’m a bit lazy today, I’ll write more tomorrow….

Remember to support me with 1 vote to give me more motivation

Monetary Types

This is a data type that stores a currency amount with the precision set:

TypeSizeAbout
money8 bytes-92233720368547758.08 to +92233720368547758.07

Character Types

Binary Data Types

Date/Time Types

Boolean Type

Enumerated Types

Network Address Types

Bit String Types

Text Search Types

UUID Type

XML Types

JSON Types

Arrays

Composite Types

Range Types

Domain Types

Object Identifier Types

pg_lsn Types

Pseudo-Types

Share the news now

Source : Viblo