Introduction
Data types are a fundamental aspect of database management systems. They define the nature of data that can be stored in a table's column. Using appropriate data types helps ensure data integrity, improves query performance, and enhances database efficiency. PostgreSQL, a powerful and versatile open-source object-relational database system, offers a wide range of data types to cater to various needs. Its extensive data type support makes it suitable for diverse applications, enabling seamless integration with other technologies.
This article shows you how to use PostgreSQL data types.
Prerequisites
Before you begin, ensure you've:
- A VPS server. We recommend a Digital Ocean VPS server.
- A PostgreSQL database server. Read our guides below to learn how to install PostgreSQL on your operating system:
- A non-root user with sudo privileges.
Set Up a Sample Database
This section walks you through the various PostgreSQL data types with examples.
-
Log in to the PostgreSQL server as the
postgres
user.CONSOLE$ sudo -u postgres psql
-
Create a database named
company
.postgresqlCREATE DATABASE company;
-
Connect to the
company
database.postgresql\c company
-
Create a table named
products
with columns to demonstrate different data types.postgresqlCREATE TABLE products ( product_id SERIAL PRIMARY KEY, product_name VARCHAR(255) NOT NULL, quantity INTEGER NOT NULL, price NUMERIC(10, 2) NOT NULL, available BOOLEAN NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
-
Insert sample data into the
products
table.postgresqlINSERT INTO products (product_name, quantity, price, available) VALUES ('Product 1', 100, 10.99, TRUE), ('Product 2', 150, 15.49, FALSE), ('Product 3', 200, 7.99, TRUE);
-
Retrieve the records from the
products
table to confirm the data has been inserted correctly.postgresqlSELECT * FROM products;
Common PostgreSQL Data Types
This section introduces ten common PostgreSQL data types, their limitations, and use cases.
-
INTEGER: Used to store whole numbers.
- Limitations: Can store values from -2,147,483,648 to 2,147,483,647.
- Use Cases: Counting items, storing age.
postgresqlCREATE TABLE example_integer ( id INTEGER );
-
SERIAL: Auto-incrementing integer.
- Limitations: Same as INTEGER.
- Use Cases: Auto-incrementing primary keys.
postgresqlCREATE TABLE example_serial ( id SERIAL PRIMARY KEY );
-
VARCHAR(n): Variable-length character type.
- Limitations: Maximum length specified by n.
- Use Cases: Storing names, descriptions.
postgresqlCREATE TABLE example_varchar ( name VARCHAR(50) );
-
TEXT: Variable-length character type without length limit.
- Limitations: No length restriction.
- Use Cases: Storing large text blocks, comments.
postgresqlCREATE TABLE example_text ( description TEXT );
-
BOOLEAN: Used to store boolean values.
- Limitations: Can store TRUE or FALSE.
- Use Cases: Flags, conditions.
postgresqlCREATE TABLE example_boolean ( is_active BOOLEAN );
-
DATE: Used to store date.
- Limitations: Stores date only.
- Use Cases: Birthdates, event dates.
postgresqlCREATE TABLE example_date ( event_date DATE );
-
TIME: Used to store time of day.
- Limitations: Stores time only.
- Use Cases: Appointment times, schedules.
postgresqlCREATE TABLE example_time ( event_time TIME );
-
TIMESTAMP: Used to store date and time.
- Limitations: Stores both date and time.
- Use Cases: Timestamps for records, logs.
postgresqlCREATE TABLE example_timestamp ( created_at TIMESTAMP );
-
NUMERIC(p, s): Exact numeric type with precision and scale.
- Limitations: Precision (p) and scale (s) must be specified.
- Use Cases: Storing financial data, prices.
postgresqlCREATE TABLE example_numeric ( price NUMERIC(10, 2) );
-
ARRAY: Used to store arrays of any data type.
- Limitations: Can store arrays of specified type.
- Use Cases: Storing multiple values in one column.
postgresqlCREATE TABLE example_array ( values INTEGER[] );
Conclusion
This guide shows you the different data types available in PostgreSQL. You learned how to log in to the PostgreSQL server, create a database, and set up a table with various columns to illustrate different data types. Additionally, you learned about ten common PostgreSQL data types, their limitations, and use cases. For further exploration, consider experimenting with more advanced data types and functions in PostgreSQL to enhance your database management skills.