PostgreSQL Data Types

PostgreSQL Data Types

PostgreSQL Data Types

PostgreSQL provides a rich set of data types to store different kinds of data efficiently. Below is a categorized list of the most commonly used PostgreSQL data types.

1. Numeric Data Types

Used for storing numbers (integers, decimals, floating points, etc.).

Data TypeAliasStorage SizeDescription
SMALLINTint22 bytesStores small integers (-32,768 to 32,767)
INTEGERint44 bytesStores standard integers (-2,147,483,648 to 2,147,483,647)
BIGINTint88 bytesStores large integers (-9 quintillion to +9 quintillion)
DECIMAL(p, s)NUMERIC(p, s)VariableStores fixed precision decimals (e.g., DECIMAL(10,2))
REALfloat44 bytesStores single-precision floating-point numbers
DOUBLE PRECISIONfloat88 bytesStores double-precision floating-point numbers
SERIALint44 bytesAuto-incrementing integer (used for primary keys)
BIGSERIALint88 bytesAuto-incrementing big integer

💡 Use NUMERIC for precise decimal values (e.g., for currency calculations).

2. Character Data Types

Used to store text-based data.

Data TypeStorage SizeDescription
CHAR(n)n bytesFixed-length character string (e.g., CHAR(10))
VARCHAR(n)VariableVariable-length character string (e.g., VARCHAR(255))
TEXTVariableUnlimited-length string (slower indexing than VARCHAR)

💡 Use TEXT for large text data and VARCHAR(n) for performance optimization.

3. Boolean Data Type

Used for storing TRUE or FALSE values.

Data TypeStorage SizeDescription
BOOLEAN1 byteStores TRUE, FALSE, or NULL

💡 Use this for flags (e.g., is_active, is_verified).

4. Date & Time Data Types

Used to store dates and timestamps.

Data TypeStorage SizeDescription
DATE4 bytesStores date (YYYY-MM-DD)
TIME8 bytesStores time (HH:MI:SS)
TIMESTAMP8 bytesStores date and time (without time zone)
TIMESTAMP WITH TIME ZONE8 bytesStores date and time (with time zone support)
INTERVAL12 bytesStores time intervals (e.g., 1 day 2 hours)

💡 Use TIMESTAMP WITH TIME ZONE for applications dealing with multiple time zones.

5. JSON Data Types

Used to store structured data in JSON format.

Data TypeStorage SizeDescription
JSONVariableStores unstructured JSON data (slow search)
JSONBVariableStores binary JSON data (faster search & indexing)

💡 Use JSONB for better performance in queries and indexing.

6. UUID (Universally Unique Identifier)

Used to store unique identifiers.

Data TypeStorage SizeDescription
UUID16 bytesStores universally unique identifiers (e.g., 550e8400-e29b-41d4-a716-446655440000)

💡 Use UUID for unique primary keys instead of SERIAL in distributed systems.

7. Array Data Type

Used to store arrays of a specific data type.

Data TypeExample
INTEGER[]{1,2,3,4}
TEXT[]{'apple', 'banana', 'cherry'}
BOOLEAN[]{TRUE, FALSE, TRUE}

💡 Use arrays when a column needs to store multiple values of the same type.

8. Network Address Data Types

Used for storing network-related data.

Data TypeStorage SizeDescription
INET7 or 19 bytesStores IPv4 or IPv6 addresses
CIDR7 or 19 bytesStores network blocks (e.g., 192.168.0.0/24)
MACADDR6 bytesStores MAC addresses (e.g., 08:00:2b:01:02:03)

💡 Use INET for storing IP addresses.

9. Geometric Data Types

Used for storing geometric shapes.

Data TypeDescription
POINTA point in 2D space (e.g., (1,2))
LINEA line in 2D space
LSEGA line segment
BOXA rectangular box
CIRCLEA circle

💡 Useful for GIS (Geographic Information System) applications.

10. XML Data Type

Used to store XML data.

Data TypeStorage SizeDescription
XMLVariableStores well-formed XML data

💡 Use XML if you need to store and query XML-based data.

11. Full-Text Search Data Types

Used for efficient text searching.

Data TypeStorage SizeDescription
TSVECTORVariableStores a document for full-text search
TSQUERYVariableStores a search query

💡 Use these for advanced text search functionalities.

12. Custom Data Types

PostgreSQL allows users to define custom data types.

Example:

CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');

Choosing the Right Data Type

  1. For large text fields → Use TEXT
  2. For small strings (e.g., names, emails) → Use VARCHAR(n)
  3. For precise decimal values (e.g., prices) → Use NUMERIC
  4. For timestamps with time zones → Use TIMESTAMP WITH TIME ZONE
  5. For unique keys → Use UUID instead of SERIAL
  6. For JSON data → Use JSONB for better performance
  7. For storing lists → Use ARRAY[]

Final Thoughts

Choosing the right data type in PostgreSQL improves performance, storage efficiency, and query speed. If you need a specific recommendation based on your use case, let me know! 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close