PostgreSQL Datatypes
Palavras-chave:
Publicado em: 05/08/2025PostgreSQL Datatypes: A Comprehensive Guide
PostgreSQL datatypes define the type of data a column can hold in a table. Choosing the right datatype is crucial for data integrity, storage efficiency, and query performance. This article provides an in-depth overview of common PostgreSQL datatypes and best practices for their use.
Fundamental Concepts / Prerequisites
Before diving into PostgreSQL datatypes, it's helpful to have a basic understanding of relational databases, tables, columns, and SQL. Familiarity with SQL syntax for creating tables and inserting data will also be beneficial.
Core Datatypes
PostgreSQL offers a rich set of built-in datatypes, categorized into several groups including Numeric, Character, Date/Time, Boolean, and Geometric. We will focus on some of the most commonly used.
Numeric Datatypes
Numeric datatypes are used to store numerical values.
-- Integer Types
CREATE TABLE employees (
id SERIAL PRIMARY KEY, -- Automatically generates unique integer sequences
age SMALLINT, -- Signed two-byte integer (-32768 to +32767)
salary INTEGER, -- Signed four-byte integer (-2147483648 to +2147483647)
big_salary BIGINT -- Signed eight-byte integer (-9223372036854775808 to +9223372036854775807)
);
-- Floating-point Types
CREATE TABLE products (
id SERIAL PRIMARY KEY,
price REAL, -- Variable-precision, inexact, typically 6 decimal digits precision
cost DOUBLE PRECISION -- Variable-precision, inexact, typically 15 decimal digits precision
);
-- Decimal Types
CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
amount NUMERIC(10, 2) -- Exact, user-specified precision and scale. 10 digits total, 2 after decimal
);
Code Explanation
The SQL code demonstrates the creation of tables utilizing various numeric datatypes. The SERIAL
datatype automatically generates a sequence of integers for the id
column, making it a convenient primary key. SMALLINT
, INTEGER
, and BIGINT
represent signed integers of different sizes. REAL
and DOUBLE PRECISION
store floating-point numbers with varying precision. NUMERIC
is used for exact numeric storage with a specified precision and scale (total digits and digits after the decimal point).
Character Datatypes
Character datatypes are used to store text.
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(255), -- Variable-length character string (up to 255 characters)
address TEXT, -- Variable-length character string (unlimited length)
code CHAR(5) -- Fixed-length character string (5 characters, padded with spaces if shorter)
);
Code Explanation
This snippet showcases the creation of a customers
table. VARCHAR(255)
stores variable-length strings, with a maximum length of 255 characters. TEXT
also stores variable-length strings but without a predefined limit (although practical limits exist based on storage). CHAR(5)
is a fixed-length string; if a string shorter than 5 characters is inserted, it will be padded with spaces.
Date/Time Datatypes
Date/Time datatypes are used to store date and time information.
CREATE TABLE events (
id SERIAL PRIMARY KEY,
event_date DATE, -- Calendar date (year, month, day)
event_time TIME, -- Time of day (hour, minute, second)
event_timestamp TIMESTAMP, -- Date and time (without time zone)
event_timestamptz TIMESTAMPTZ -- Date and time (with time zone)
);
Code Explanation
The events
table stores date and time information. DATE
stores only the date. TIME
stores only the time. TIMESTAMP
stores both date and time without time zone information. TIMESTAMPTZ
stores both date and time along with time zone information, making it generally preferable for applications requiring accurate global time tracking.
Boolean Datatype
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
description TEXT,
is_completed BOOLEAN -- Stores TRUE or FALSE values
);
Code Explanation
This example presents the BOOLEAN
datatype in the tasks
table. It's used to store true/false or yes/no values, represented as TRUE
or FALSE
.
Complexity Analysis
The choice of datatype primarily impacts storage complexity. Smaller datatypes (like SMALLINT
or CHAR(n)
with a small `n`) require less storage than larger datatypes (like BIGINT
or TEXT
). Time complexity is indirectly affected; queries involving larger datatypes or complex operations might take longer to execute than queries involving smaller datatypes and simpler operations. For example, searching within a TEXT column can be slower than searching within an INTEGER column.
Alternative Approaches
While the built-in datatypes cover most common use cases, PostgreSQL also supports custom datatypes using composite types and domains. Composite types allow grouping multiple attributes under a single type. Domains create subtypes based on existing datatypes, enforcing additional constraints. These approaches can provide greater flexibility and data validation, but also increase complexity.
Conclusion
Selecting the appropriate datatype in PostgreSQL is essential for database efficiency and integrity. By understanding the different datatype categories and their characteristics, developers can optimize storage, improve query performance, and ensure data accuracy. Consider the size and nature of the data when choosing between various numeric, character, date/time, and other datatypes. For accurate global timestamp management, use TIMESTAMPTZ
. Remember to trade off complexity against benefits when considering custom datatypes.