Sub topic 17
Choosing Types for a Schema
Assume you know how to write basic SELECT, INSERT, and CREATE TABLE statements. This chapter focuses on choosing types that preserve meaning, enforce valid data, and support the queries your application needs.
A column type is part of your data model. It controls which values PostgreSQL accepts, how values are stored, which operators work, and how comparisons behave. Choose the narrowest type that correctly represents the value, but do not choose a type so narrow that valid future values will fail.
Start with the meaning of the value
Ask these questions for every column:
- Is the value an identifier, a measurement, a label, a time, or a document?
- Does it need exact arithmetic?
- Is the set of valid values fixed?
- Will queries inspect parts of the value?
- Should the column allow
NULL? - Can a constraint express an important business rule?
| Value | Usually suitable type | Reason |
|---|---|---|
| Generated row number | `bigint` identity | Large range and automatic generation |
| Price or account balance | `numeric(12,2)` | Exact decimal arithmetic |
| Free-form name | `text` | No artificial length limit |
| Moment in time | `timestamptz` | Represents an instant consistently |
| Flexible attributes | `jsonb` | Structured, queryable document |
| Several labels | `text[]` | Compact collection when relationships are simple |
Build the schema from those decisions
Consider an order table:
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
public_id uuid NOT NULL UNIQUE,
customer_email text NOT NULL,
total numeric(12,2) NOT NULL CHECK (total >= 0),
status text NOT NULL
CHECK (status IN ('pending', 'paid', 'cancelled')),
is_gift boolean NOT NULL DEFAULT false,
tags text[] NOT NULL DEFAULT '{}',
details jsonb NOT NULL DEFAULT '{}',
placed_at timestamptz NOT NULL DEFAULT now()
);The identity column is a database-generated internal key. A uuid can provide an identifier that is harder to guess when it is exposed outside the database. Use numeric for money because floating-point values can contain rounding errors.
text is usually the right choice for ordinary strings. varchar(n) adds a maximum length, but a CHECK constraint can express the same rule more clearly when the limit is a business requirement. char(n) pads values with spaces and is rarely useful for new schemas.
The status constraint works well when the values are local to one table and may change. An enum can be appropriate for a small, stable vocabulary shared across many columns. A separate lookup table is better when values need descriptions, ordering, translations, or frequent changes.
Choose time and document types carefully
Use timestamptz for an event or deadline that represents a real instant. PostgreSQL stores the instant and displays it using the session time zone. Use timestamp without time zone only for a wall-clock value whose location is handled separately, such as “the store opens at 09:00.”
Use jsonb when attributes vary between rows or when the document structure belongs to the application. Do not put ordinary relational columns into JSONB merely to avoid designing them. Relational columns provide clearer constraints and simpler joins.
Exercise
Which type should store the exact amount of a payment, and which type should store when the payment occurred?
Show answer
Use numeric(12,2) for the payment amount and timestamptz for the time because the amount needs exact arithmetic and the event is an instant.