Introduction
PostgreSQL has many data types to help store different kinds of information easily and correctly.
PostgreSQL has many data types to help store different kinds of information easily and correctly.
CREATE TABLE table_name ( column_name data_type );
CREATE TABLE events ( id SERIAL PRIMARY KEY, event_name TEXT, event_date DATE );
CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT, tags TEXT[] );
CREATE TABLE users ( id SERIAL PRIMARY KEY, profile JSONB );
This example creates a books table with text, date, and JSONB columns. It inserts two books and then selects all rows.
CREATE TABLE books ( id SERIAL PRIMARY KEY, title TEXT NOT NULL, published_date DATE, metadata JSONB ); INSERT INTO books (title, published_date, metadata) VALUES ('Learn SQL', '2023-01-15', '{"pages": 250, "language": "English"}'), ('PostgreSQL Guide', '2022-11-10', '{"pages": 300, "language": "English"}'); SELECT * FROM books;
Using the right data type helps PostgreSQL check your data and run queries faster.
PostgreSQL's rich data types let you store complex data without extra work.
PostgreSQL offers many data types to store different kinds of data easily.
Choosing the right data type keeps data accurate and queries efficient.
Rich data types help handle complex and flexible data in real life.
json.json type; others are invalid or do not exist.CREATE TABLE measurements (id SERIAL, data jsonb); and the insert INSERT INTO measurements (data) VALUES ('{"temp": 22, "unit": "C"}');, what will the query SELECT data->>'temp' FROM measurements; return?->> extracts the JSON value as text, so data->>'temp' returns the value of key 'temp' as text.CREATE TABLE users (id INT, preferences jsonb DEFAULT '{}');'{}'::jsonb to be valid.Pass the test and this pattern moves unknown