Data Types

Data Types

Boolean Data

  • TRUE

  • FALSE

  • NULL

TRUE
FALSE

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 Type
Notes

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

Types
Notes

Integers

whole number, +ve and -ve

Fixed-point, floating point

for fractions of whole nu

type
size (bytes)
min
max

smallint

2

-32678

32767

integer

4

-2,147,483,648

2,147,483,647

bigint

8

-9223372036854775808

9223372036854775807

type
size
range

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

Type
Notes

Real

allows precision to six decimal digits

Double precision

allows precision to 15 digits points of precision

type
size
storage type
Range

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

type
stores
low
high

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 zone

  • timestamptz : timestamp with time zone , stored using UTC format

  • adding 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

Name
Storage Size
Notes

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?