Data Types
Data Types
Boolean Data
TRUE
FALSE
NULL
TRUE
FALSE
'true'
'false'
't'
'f'
'yes'
'no'
'y'
'n'
'1'
'0'
CREATE TABLE booltable (
id SERIAL PRIMARY KEY ,
is_enable BOOLEAN NOT NULL
);
INSERT INTO booltable (is_enable) VALUES (TRUE), ('true'),
('y') , ('yes'), ('t'), ('1');
INSERT INTO booltable (is_enable) VALUES (FALSE), ('false'),
('n') , ('no'), ('f'), ('0');
SELECT * FROM booltable;
SELECT * FROM booltable WHERE is_enable = 'y';
SELECT * FROM booltable WHERE NOT is_enable;Character Data
CHARACTER (N), CHAR (N)
fixed-length, blank padded
CHARACTER VARYING (N), VARCHAR(N)
variable length with length limit
TEXT, VARCHAR
variable unlimited length, max 1GB
n is default to 1
Numeric Data
Integers
whole number, +ve and -ve
Fixed-point, floating point
for fractions of whole nu
smallint
2
-32678
32767
integer
4
-2,147,483,648
2,147,483,647
bigint
8
-9223372036854775808
9223372036854775807
smallserial
2
1 to 32767
serial
4
1 to 2147483647
bigserial
8
1 to 9223372036854775807
Fixed Point Data
numeric ( precision , scale ) | decimal ( precision , scale )
precision : max number of digits to the left and right of the decimal point
scale : number of digits allowable on the right of the decimal point
Floating Point Data
Real
allows precision to six decimal digits
Double precision
allows precision to 15 digits points of precision
numeric, decimal
variable
fixed point
131072 digits before decimal point and 16383 digits after the decimal point
real
4
floating point
6 decimal digits precision
double precision
8
floating point
15 decimal digits precision
Hierarchical order to SELECT best type : numeric > decimal > float
Date Time Data
Date
date only
4713 BC
294276 AD
Time
time only
4713 BC
5874897 AD
Timestamp
date and time
4713 BC
294276 AD
Timestampz
date, time and timezone
4713 BC
294276 AD
Interval
difference btw time
Date type
Time type
Timestamp and Timezone
timestamp: stores time without time zonetimestamptz: timestamp with time zone , stored using UTC formatadding timestamp to timestamptz without mentioning the zone will result in server automatically assumes timezone to system's timezone
Internally, PostgreSQL will store the timezoneaccurately but then OUTPUTting the data, will it be converted according to your timezone
UUID
UUID : Universal Unique Identifier
PostgreSQL doesn't provide internal function to generate UUID's, use
uuid-ossp
HSTORE
stores data in key-value pairs
key and VALUES are text string only
Json
PostgreSQL supports both
JSON
BSON or JSONB ( Binary JSON )
JSONB has full support for indexing
Network Address Data Types
cidr
7 or 19 bytes
IPv4 and IPv6 networks
inet
7 or 19 bytes
IPv4 and IPv6 hosts and networks
macaddr
6 bytes
MAC addresses
macaddr8
8 bytes
MAC addresses ( EUI 64-bit )
It is better to use these types instead of plain text types of store network address, because these types offer input error checking and specialised operators and functions
Supports indexing and advance operations
Last updated
Was this helpful?