Data Types Basics
10 examples to get you started with PostgreSQL data types - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with PostgreSQL data types - 7 basic and 3 intermediate.
Connected to PostgreSQL 18 with CREATE privilege on a scratch schema.
# Local dev with Docker (PostgreSQL 18)
docker run -d --name pg18 -e POSTGRES_PASSWORD=dev -p 5432:5432 postgres:18
psql postgres://postgres:dev@localhost:5432/postgresbigint for IDs; int for counts; smallint for enums-as-int.
CREATE TABLE app.counts (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
score int NOT NULL CHECK (score BETWEEN 0 AND 100)
);text has no performance penalty vs varchar(n) in PostgreSQL.
CREATE TABLE app.labels (
code text NOT NULL UNIQUE,
title text NOT NULL
);Three-valued if nullable; prefer NOT NULL with default.
ALTER TABLE app.customers ADD COLUMN is_active boolean NOT NULL DEFAULT true;numeric(p,s) for exact decimal arithmetic.
CREATE TABLE app.payments (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
amount numeric(12,2) NOT NULL CHECK (amount >= 0)
);Store instants in UTC.
CREATE TABLE app.events (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
occurred_at timestamptz NOT NULL DEFAULT now()
);uuid type with gen_random_uuid().
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE app.tokens (
id uuid PRIMARY KEY DEFAULT gen_random_uuid()
);Binary JSON with indexing operators.
CREATE TABLE app.profiles (
user_id bigint PRIMARY KEY,
attrs jsonb NOT NULL DEFAULT '{}'::jsonb
);
INSERT INTO app.profiles VALUES (1, '{"tier":"pro"}');Closed set of labels stored as OIDs.
CREATE TYPE app.plan_tier AS ENUM ('free','pro','enterprise');
ALTER TABLE app.customers ADD COLUMN tier app.plan_tier NOT NULL DEFAULT 'free';Postgres arrays for tags (use carefully).
ALTER TABLE app.customers ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
CREATE INDEX customers_tags_gin ON app.customers USING gin (tags);Reusable CHECK on a base type.
CREATE DOMAIN app.email AS text
CHECK (VALUE ~* '^[^@]+@[^@]+\.[^@]+$');
ALTER TABLE app.customers ALTER COLUMN email TYPE app.email USING email::app.email;Stack versions: This page was written for PostgreSQL 18.4 (stable 18, maintenance 17), pgvector 0.8+, PgBouncer 1.x, Patroni 3.x, and PostGIS 3.5+.
Reviewed by Chris St. John·Last updated Jul 16, 2026