Skip to main content

Introduction of datatypes in PostgreSQL

Before going to learn the next steps like create database, table, function and its other thing we have to know about the datatypes, so in this article we will learn about the datatypes. It will be beneficial for us when we are doing architecture design in PostgreSQL, it will give several benefits like consistency, Validation, Compactness, and performance. Consistency means we have to choose proper datatype for a column so that we can get the consistent and fastest result. Validation means, if we use proper datatype for a column we can easily validate data which is out of scope from the outside. Compactness means, we will use appropriate datatype as per required data, if data comes in single value then we have to choose data type accordingly. Proper datatype according to data scope will increase the performance which is a very important factor in any database architecture design. PostgreSQL supports a wide set of the datatype, it also provides the facility to create user-defined datatype as per user requirement.
Let’s start a brief description of datatypes in PostgreSQL.
smallint: it is small range integer, can store 2bytes of data which range is -32768 to +32767.
integer: its storage size is 4bytes and range is -2147483648 to +2147483647
bigint: it is large range integer; the range is -9223372036854775808 to 9223372036854775807.
decimal: range is up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.
numeric: range is up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.
real: 4bytes storage size and range is 6 decimal digits precision.
double precision: 8-bytes storage size and range is 15 decimal digits precision.
smallserial: 2-byte storage size and range is 1 to 32767.
serial: 4 bytes storage size and the range is 1 to 2147483647.
bigserial: 8 bytes storage size and the range is 1 to 9223372036854775807.
money: 8 bytes storage size and the range is -92233720368547758.08 to +92233720368547758.07.
------Character type--------
character varying(n): Variable-length bit string, variable length with the limit.
character varchar(n): Variable-length bit string, variable length with the limit.
character(n): fixed-length character string.
char(n): fixed-length character string.
-------Binary Data Types---------
bytea: 1 or 4 bytes plus the actual binary string storage size
-------Date/Time data types---------
timestamp[(p)][without time zone]: 8 bytes storage size, it's contain both date and time(no time zone).
timestamp[(p)][with time zone]: 8 bytes storage size, it's contain both date and time(with  time zone).
date: 4 bytes storage size, its contains date only (not a time of the day)
time[(p)][without time zone]: 12 bytes storage size, its contains times of demurely without time zone.
time[(p)][with time zone]: 12 bytes storage size, its contains times of demurely with time zone.
interval [fields ] [(p)]: 12 bytes storage size it contains time intervals.
------Boolean Type------
boolean: 1-byte storage size, state of true or false.
-------- Geometric Type-------
geometric data types represent two-dimensional spatial objects
point: 16 bytes storage size, its contain the coordinates of the two-dimensional blocks. For example (x, y) x is latitude and y is longitude, x and y is floating points.
line: 32 bytes storage size, the line connects the two points, so this data types column contains the two points as like [(x1, y1), (x2, y2)].
lseg: 32 bytes storage size, the line connects the two points, so this data types columns contains the two points as like [(x1, y1), (x2, y2)] online. Suppose (x1,y1) and (x2, y2) is line points and (x4, y4) and (x5,y5) belongs to the line, it means (x4, y4) and (x5, y5) is a segment of the line.
box: 32 bytes storage size, box means a geometrical draw of rectangular and square. boxes are represented by pairs of points that the opposite corner of the box value of type box are specified using the sentence like ((x1, y1), (x2, y2)).
Where (x1, y1) and (x2, y2) are two opposite corners of the box.
path: 16 bytes storage size and can be extended as 16+16n, path are represented by list of connected. Fast can be open where the first and last point in the list are considered not committed or closed where the first and last point are considered connected.
[(X1, Y1), (X2, Y2),…….(Xn, Yn)]
polygon: 40 + 16n storage size, polygons are very similar to the closed path but are stored differently and have their own set of support routines
[(X1, Y1), (X2, Y2),…….(Xn, Yn)]
circle: 24 bytes storage size, Circle means, a point and a radius and used like as <(x1, y1), r>.
-----------------------Network Address data types----------------------
PostgreSQL provides facility to store Ipv4, IPv6, and Mac addresses. It is better to use this type instead of plain Text type to store network addresses because this time offers input error checking any specialized operators and functions.
cidr: 7 bytes storage size, can store Ipv4 and Ipv6 networking data.
inet: 7 bytes storage size, can store Ipv4 and Ipv6 host networking data.
macaddr: 6 bytes storage size, can use to store MAC address


Popular posts from this blog

Add day to ISODate in MongoDB

We can use $add operator to add days in ISODate in mongodb, $add is the Arithmetic Aggregation Operator which adds number and date in mongodb.
Syntax:

{ $add: [ <expression1>, <expression2>, ... ] }

Note:  If one of the argument is date $add operator treats to other arguments as milliseconds to add to the date.
Example: Suppose we have a Test collection as below.

{"Title" : "Add day to ISODate in MongoBD","CreatedDate" : ISODate("2016-07-07T08:00:00.000Z")}

Query to add 2 days in CreatedDate

db.Test.aggregate([      { $project: { Title: 1, AddedDate: { $add: [ "$CreatedDate", 2*24*60*60000 ] } } }    ])

Result:

{ "_id" : ObjectId("579a1567ac1b3f3732483de0"), "Title" : "Add day to ISODate in MongoBD", "AddedDate" : ISODate("2016-07-09T08:00:00.000Z") }

Note: As mentioned in above note we have to convert days in millisecond because $add operator treat to other arg…

Remove special character from string in MongoDB

Problem: Suppose wehave a collection and one field is type string contains some special character (like !@#$%) and we don’t want these special character.
Solution: We can easily remove the special character from field using script “replace(/[^a-zA-Z 0-9 ]/g, '')” in our query.  How can we remove special character from string using this script please see following example.
Example: Suppose we have a collection “EduSurvey “where we are collecting information from institutions.

{Name:"JB institute”, About:"This is good one collage for MBA", Information:"This $%%institute ##has good faculty etc$$"}
{Name:"MK institute”, About:"This is good one collage for MCA", Information:"This$$%# is the dummy text12"}
{Name:"MG institute”, About:"This is good one collage for B,Tech", Information:"This# institute@ has&* good infrastructure"}

Did you notice Information fields contains some special character so we…