PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Tutorial / PostgreSQL Data Types

PostgreSQL Data Types

Summary: in this tutorial, you will learn about PostgreSQL data types including Boolean, character, numeric, temporal, array, json, uuid, and special types.

PostgreSQL Data TypesOverview of PostgreSQL data types

PostgreSQL supports the following data types:

  • Boolean
  • Character types such as char, varchar, and text.
  • Numeric types such as integer and floating-point number.
  • Temporal types such as date, time, timestamp, and interval
  • UUID for storing Universally Unique Identifiers
  • Array for storing array strings, numbers, etc.
  • JSON stores JSON data
  • hstore stores key-value pair
  • Special types such as network address and geometric data.

Boolean

A Boolean data type can hold one of three possible values: true, false or null. You use boolean or bool keyword to declare a column with the Boolean data type.

When you insert data into a Boolean column, PostgreSQL converts it to a Boolean value e.g., 1, yes, y, t, true are converted to true, and 0, no, n false, f are converted to false.

When you select data from a Boolean column, PostgreSQL converts the value back e.g., t to true, f to false and space to null.

Character

PostgreSQL provides three character data types: CHAR(n), VARCHAR(n), and TEXT

  • CHAR(n) is the fixed-length character with space padded. If you insert a string that is shorter than the length of the column, PostgreSQL pads spaces. If you insert a string that is longer than the length of the column, PostgreSQL will issue an error.
  •  VARCHAR(n) is the variable-length character string.  With VARCHAR(N),  you can store up to n characters. PostgreSQL does not pad spaces when the stored string is shorter than the length of the column.
  •  TEXT is variable-length character string. Theoretically, text data is a character string with unlimited length.

Numeric

PostgreSQL provides two distinct types of numbers:

  • integers
  • floating-point numbers

Integer

There are three kinds of integers in PostgreSQL:

  • Small integer ( SMALLINT)  is 2-byte signed integer that has a range from -32768 to 32767.
  • Integer ( INT) is 4-byte integer that has a range from -214783648 to -214783647.
  • Serial is the same as integer except that PostgreSQL will automatically generate and populate values into the SERIAL column. This is similar to AUTO_INCREMENT column in MySQL or AUTOINCREMENT column in SQLite

Floating-point number

There three main types of floating-point numbers:

  • float(n)  is a floating-point number whose precision, at least, n, up to a maximum of 8 bytes.
  • realor float8is a double-precision (8-byte) floating-point number.
  • numeric or numeric(p,s) is a real number with p digits with s number after the decimal point. The numeric(p,s) is the exact number.

Temporal data types

The temporal data types allow you to store date and /or  time data. PostgreSQL has five main temporal data types:

  • DATEstores date values only.
  • TIMEstores time of day values.
  • TIMESTAMPstores date and time.
  • TIMESTAMPZ stores both timestamp and time zone data.
  • INTERVAL stores periods of time.

The TIMESTAMPZ is the PostgreSQL’s extension to the SQL standard’s temporal data types.

Arrays

In PostgreSQL, you can store an array of strings, an array of integers, etc., in array columns. The array comes to handy in some situations e.g., storing days of the week, months of the year, etc.

JSON

PostgreSQL provides two JSON data types: JSON and JSONB for storing JSON data.

The JSON data type stores plain JSON data that requires reparsing for each processing, while JSONB data type stores JSON data in a binary format which is faster to process but slower to insert. In addition, JSONB supports indexing, which can be an advantage.

UUID

The UUID data type allows you to store Universal Unique Identifiers defined by RFC 4122 . The UUID values guarantee a better uniqueness than SERIAL and can be used to hide sensitive data exposed to public such as values of id in URL, etc.

Special data types

Besides the primitive data types, PostgreSQL also provides several special data types related to geometric and network.

  • box– a rectangular box.
  • line – a set of points.
  • point– a geometric pair of numbers.
  • lseg– a line segment.
  • polygon– a closed geometric.
  • inet– an IP4 address.
  • macaddr– a MAC address.

In this tutorial, we have introduced you to the PostgreSQL data types so that you can use them to create tables in the next tutorial.

Related Tutorials

  • Understanding PostgreSQL Timestamp Data Types
  • PostgreSQL Boolean Data Type with Practical Examples
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
Previous Tutorial: PostgreSQL DELETE
Next Tutorial: PostgreSQL CREATE TABLE

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.