PostgreSQL Data Types

Discover the diverse world of PostgreSQL data types with our comprehensive guide. From numeric and character types to date/time, boolean, arrays...

In the realm of database management systems, PostgreSQL stands tall as a versatile and feature-rich option, offering a diverse range of data types to suit various data storage needs. From basic numeric and text types to specialized types for spatial and JSON data, PostgreSQL provides a robust foundation for efficient data management. 

PostgreSQL Data Types

In this comprehensive guide, we'll explore PostgreSQL data types in depth, accompanied by practical examples to illustrate their usage and benefits.

Introduction to PostgreSQL Data Types

PostgreSQL data types encompass a wide range of classifications that dictate the nature of data stored and manipulated within the database management system. These types serve as the cornerstone for structuring and interpreting information stored in tables, offering a framework that accommodates diverse data formats and structures. With PostgreSQL's extensive and varied collection of data types meticulously organized into distinct sets, users are provided with unparalleled flexibility. This comprehensive assortment facilitates precise representation and manipulation of various data forms, whether it involves handling numeric intricacies, managing textual information, or addressing specialized requirements such as spatial data. By offering a feature-rich landscape, PostgreSQL empowers users to navigate the complexities of data management with finesse and adaptability, underscoring its commitment to delivering a versatile and powerful environment for data storage and retrieval.

Let's delve into an exploration of PostgreSQL data types, unraveling their intricacies one by one:

1. Numeric Data Types

Numeric data types in PostgreSQL are used to store numerical values with varying precision and range. These data types are essential for handling numerical data in various applications, including financial calculations, scientific research, and data analysis. 

PostgreSQL offers a range of numeric data types to accommodate different requirements:

Numeric Type Description Storage Size Range
INTEGER Standard integer 4 bytes -2,147,483,648 to 2,147,483,647
SMALLINT Small integer 2 bytes -32,768 to 32,767
BIGINT Large integer 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
REAL Single-precision floating-point 4 bytes 1.17549e-38 to 3.40282e+38
DOUBLE PRECISION Double-precision floating-point 8 bytes 2.22507e-308 to 1.79769e+308
NUMERIC/DECIMAL Fixed-point with user-defined precision Variable Variable based on precision and scale
SERIAL Auto-incrementing integer (typically INT) 4 bytes 1 to 2,147,483,647
BIGSERIAL Auto-incrementing large integer (typically BIGINT) 8 bytes 1 to 9,223,372,036,854,775,807

2. Character Data Types

Character data types in PostgreSQL are used to store textual information. These types are designed to handle strings and characters of varying lengths. 

Below is the table which represents various character data types in PostgreSQL:

Character Type Description Storage Size Example
CHAR(n) Fixed-length character string n characters CHAR(10)
VARCHAR(n) Variable-length character string Actual length (up to n characters) VARCHAR(255)
TEXT Variable-length character string without a specified maximum length Actual length TEXT

3. Monetary Data Type 

The Monetary data type in PostgreSQL is designed to handle currency values, providing a specialized format for storing monetary amounts. This data type ensures precision and consistency in representing financial figures, which is crucial for applications dealing with monetary transactions, accounting, and financial analysis.

Name Description Storage Size Range
MONEY Monetary values 8 bytes Range varies based on precision and scale

4. Binary Data Type

Binary data types in PostgreSQL are designed to store binary data, which can include images, audio files, video files, or any other type of data in binary format. 

Name Storage Size Description
BYTEA Variable Variable-length binary string

5. Date/Time Data Types

The Date/Time data types in PostgreSQL are designed to handle various aspects of temporal information, including dates, times, and intervals. Here are some common Date/Time data types in PostgreSQL:

Name Description Storage Size Minimum Value Maximum Value Resolution
DATE Date without time 4 bytes 4713 BC 5874897 AD 1 day
TIME Time without date 8 bytes 00:00:00 24:00:00 1 microsecond
TIMESTAMP Date and time 8 bytes 4713 BC 294276 AD 1 microsecond
TIMESTAMPTZ Date, time, and time zone 8 bytes 4713 BC 294276 AD 1 microsecond
INTERVAL Duration of time 16 bytes -178000000 years 178000000 years 1 microsecond

6. Boolean Data Type

The Boolean data type in PostgreSQL are used to represent truth values, typically denoted as either true or false. Boolean values are commonly used in logical expressions and conditional statements. 

The following table provides a concise representation of the Boolean data type in PostgreSQL:

Data Type Description Storage Size Example
BOOLEAN Truth value (true/false) 1 byte true or false

7. Enumerated Data Type 

Enumerated data types, commonly referred to as enums, in PostgreSQL provide a structured and efficient means to define a specific set of named values for a column. These types enable the creation of a customized enumeration, presenting a clear and organized representation of distinct choices within the database. Enumerated data types establish a fixed and ordered list of values, serving as a predefined domain for the associated column.

Data Type Description Storage Size Example
ENUM Enumerated type with a defined set of values Varies Example: ('Red', 'Green', 'Blue')

8. Geometric Data Types 

Geometric data types in PostgreSQL provide a set of specialized data types for representing and working with geometric shapes in a two-dimensional space. These data types are particularly useful in applications dealing with spatial data, such as GIS (Geographic Information Systems) applications.

Name Storage Size Representation Description
Point 16 bytes (x, y) Represents a point in 2D space
Line 32 bytes [(x1, y1), (x2, y2)] Represents a straight line segment
Lseg 32 bytes [(x1, y1), (x2, y2)] Represents an infinite line
Box 32 bytes ((x1, y1), (x2, y2)) Represents a rectangular box
Path Varies [(x1, y1), ... , (xn, yn)] Represents a connected sequence of points
Polygon Varies ((x1, y1), ... , (xn, yn)) Represents a closed path
Circle 24 bytes <(x, y), radius> Represents a circle

9. Network Address Types

In PostgreSQL, network address data types are used to represent and store IP addresses and MAC addresses. These data types are particularly useful when working with network-related information.

Data Type Description Storage Size Example
INET IPv4 and IPv6 addresses 4 or 16 bytes '192.168.1.1' or '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
CIDR IP address range in CIDR notation 4 or 16 bytes '192.168.1.0/24' or '2001:0db8::/32'
MACADDR MAC address 6 bytes '00:1a:2b:3c:4d:5e'

10. Bit String Data Types

The bit string data types in PostgreSQL are used to represent and manipulate fixed-length and variable-length bit strings. These data types are useful for storing binary data and implementing bitwise operations. 

Data Type Description Storage Size Example
BIT(n) Fixed-length bit string n bits BIT(8) stores 8 bits (1 byte)
BIT VARYING(n) Variable-length bit string Actual length (rounded up to the nearest byte) + 4 bytes for length information BIT VARYING(16) can store up to 16 bits (2 bytes)

11. Text Search Data Types

The text search data types in PostgreSQL are used to support full-text search capabilities within the database. These data types enable efficient searching and indexing of textual data for various applications, including search engines, document management systems, and content-based filtering. 

Data Type Description Storage Size Example
TSVECTOR Optimized document format for full-text search Varies 'search engine', 'document', 'management', 'system'
TSQUERY Search query format compatible with full-text search Varies 'search & engine'

12. UUID Data Type

In PostgreSQL, the UUID data type is used to store Universally Unique Identifiers. UUIDs are 128-bit values that are unique across both space and time, making them suitable for various scenarios where globally unique identifiers are required. 

The following table provides a  representation of the UUID data type in PostgreSQL:

Data Type Description Storage Size Example
UUID Universally Unique Identifier 16 bytes '550e8400-e29b-41d4-a716-446655440000'

13. The XML Data Type

The XML data type in PostgreSQL is used to store XML (eXtensible Markup Language) data. XML is a widely-used markup language for representing structured data, making the XML data type valuable for applications dealing with complex and hierarchical information.

Data Type Description Storage Size Example
XML Extensible Markup Language Variable
<book>
  <title>PostgreSQL Basics</title>
  <author>John Doe</author>
  <publication_year>2022</publication_year>
</book>
        

14. JSON Data Types

JSON (JavaScript Object Notation) data types are used to store JSON-formatted data, which is a lightweight data-interchange format. JSON is commonly used for representing structured data in a human-readable format. PostgreSQL provides several JSON data types to handle different aspects of JSON data.

Data Type Description Storage Size Example
JSON JavaScript Object Notation Variable
{"name": "John Doe", "age": 30, "city": "New York"}
        
JSONB Binary JSON Variable
{"name": "Jane Doe", "age": 25, "city": "San Francisco"}
        

15. Arrays

In PostgreSQL, arrays are used to store collections of elements of the same data type. Arrays can be one-dimensional, multi-dimensional, or even jagged (arrays within arrays).

Array Type Description Example
One-Dimensional Array An array with elements in a single dimension
{1, 2, 3, 4, 5}
Multi-Dimensional Array An array with elements arranged in multiple dimensions
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
        
Jagged Array An array of arrays with varying lengths
{{1, 2},
{3, 4, 5},
{6, 7, 8, 9}}
        

16. Composite Data Type

Composite data types in PostgreSQL allow you to create custom structures to organize and store related pieces of data as a single unit. These types are similar to structs in programming languages and provide a way to encapsulate multiple fields, each with its own data type, into a cohesive whole.

17. Range Data Types

In PostgreSQL, range data types allow you to represent a range of values for different data types, such as integers, numerics, timestamps, and dates. These data types provide a concise and expressive way to store and query ranges of values within the database. PostgreSQL supports several built-in range types, and users can also define their custom range types. Below is an table with information on some of the built-in range types in PostgreSQL, including their names and descriptions:

Range Type Description
int4range Range of integers (4-byte)
int8range Range of integers (8-byte)
numrange Range of numeric values
tsrange Range of timestamp without time zone values
tstzrange Range of timestamp with time zone values
daterange Range of date values

18. Domain Data  Types

The domain data types in PostgreSQL provide a way to create user-defined data types based on existing built-in data types. A domain allows you to enforce constraints, provide a meaningful name, and add documentation to an existing data type. This can improve clarity, maintainability, and data integrity within a database.

19. Object Identifier Types

The Object Identifier (OID) types are used to represent object identifiers, which are unique identifiers associated with objects in the database. OIDs are integer values that uniquely identify system objects such as tables, indexes, and large objects. However, it's important to note that the use of OIDs for user-defined tables is deprecated in recent versions of PostgreSQL, and the WITH OIDS option is no longer recommended.

Name References Description Value Example
oid any numeric object identifier 564182
regclass pg_class relation name pg_type
regcollation pg_collation collation name "POSIX"
regconfig pg_ts_config text search configuration english
regdictionary pg_ts_dict text search dictionary simple
regnamespace pg_namespace namespace name pg_catalog
regoper pg_operator operator name +
regoperator pg_operator operator with argument types *(integer,​integer) or -(NONE,​integer)
regproc pg_proc function name sum
regprocedure pg_proc function with argument types sum(int4)
regrole pg_authid role name smithee

20. pg_lsn Type

The pg_lsn type stands for "Log Sequence Number." It is a data type used to represent a position or location in the PostgreSQL write-ahead log (WAL). The WAL is a critical component of PostgreSQL's transaction processing and crash recovery mechanisms.

21. Pseudo Data Types

The pseudo types refers to a set of data types that are not actual data storage types but are used for specific purposes within the database system. These pseudo types are often employed in function and procedure signatures to convey additional information or to handle special cases. 

Name Description
RECORD Represents a row or a set of columns with varying data types. Used when the structure is not known at compile time.
ANYELEMENT Indicates that a function or operator can accept any data type. Used for generic functions.
ANYARRAY Indicates that a function or operator can accept an array of any data type. Used for generic functions working with arrays.
ANYNONARRAY Indicates that a function or operator can accept any non-array data type. Used for generic functions excluding arrays.
ANYENUM Indicates that a function or operator can accept any enumerated type. Used for generic functions with enum support.
ANYRANGE Indicates that a function or operator can accept any range type. Used for generic functions working with range types.
INTERNAL Indicates that a function or operator returns an internal system type. Used for special internal purposes.
LANGUAGE_HANDLER Indicates that a function is a language handler. Used in procedural language implementations.
TSM_HANDLER Indicates that a function is a text search method handler. Used in custom text search method implementations.
POLICY Indicates that a function or operator returns a row-level security policy. Used for managing access control policies.
FDW_HANDLER Indicates that a function is a foreign data wrapper handler. Used for implementing foreign data wrappers.
PSEUDOTYPE Indicates that a function or operator returns a special pseudo-type. Used for various purposes.
NONE Represents no specific data type. Used in certain contexts to indicate absence or unspecified type.
UNKNOWN Indicates an unknown or unspecified value. Used when the actual value is not available.
VOID Indicates a function with no specified return type. Used for functions with side effects but no result.
TRIGGER Represents the result of a trigger function. Used in the context of triggers and their effects.
EVENT_TRIGGER Indicates a special type for event trigger functions. Used in event trigger definitions.
FDW_DESCRIPTOR Represents a foreign data wrapper descriptor. Used in the internals of foreign data wrappers.
INDEX_AM_HANDLER Indicates an index access method handler. Used in the internals of index access methods.
TSVECTOR Represents a vector of text search terms. Used in full-text search functionalities.
PG_LSN Represents a Log Sequence Number, used for tracking changes in the PostgreSQL write-ahead log (WAL).
TXID_SNAPSHOT Represents a snapshot of transaction IDs for multi-version concurrency control (MVCC).
PG_NDISTINCT Represents the number of distinct values in an aggregation state.
PG_DEPENDENCIES Represents the dependencies between objects in the database.
PG_MCV_LIST Represents the most common values list for statistics calculation.
PG_INHERITS Represents the inheritance hierarchy of a table.
PG_BLOCK Represents a disk block in PostgreSQL. Used in low-level operations.
PG_VARIABLE Represents a configuration parameter. Used for dynamic configuration settings.
PG_ATTRIBUTE Represents a table column's attribute. Used in the system catalog.

Conclusion

Understanding PostgreSQL data types is essential for effectively designing database schemas, optimizing performance, and ensuring data integrity. By leveraging the diverse range of data types offered by PostgreSQL, developers can build robust and efficient database solutions tailored to their application's requirements.