// PostgreSQL Data Types
PostgreSQL Data Types
คู่มืออ้างอิง Data Types ครบถ้วน — PostgreSQL มี Built-in Types มากกว่าฐานข้อมูลอื่นใด และยังสร้าง Custom Types ได้เองอีกด้วย
// Numeric Types
ประเภทตัวเลข
| Type | Storage | Range | การใช้งาน |
|---|---|---|---|
SMALLINT | 2 bytes | -32,768 ถึง 32,767 | ค่าตัวเลขเล็กน้อย เช่น Age, Rating |
INTEGER | 4 bytes | -2.1B ถึง 2.1B | Primary Keys, General Numbers |
BIGINT | 8 bytes | ±9.2 × 10¹⁸ | Large IDs, Financial, Timestamps |
NUMERIC(p,s) | Variable | ถึง 131072 digits | การเงิน ต้องการความแม่นยำ 100% |
REAL | 4 bytes | 6 decimal digits | Scientific calculations (approximate) |
DOUBLE PRECISION | 8 bytes | 15 decimal digits | High-precision floating point |
SMALLSERIAL | 2 bytes | 1 ถึง 32,767 | Auto-increment Small ID |
SERIAL | 4 bytes | 1 ถึง 2.1B | Auto-increment ID (legacy) |
BIGSERIAL | 8 bytes | 1 ถึง 9.2×10¹⁸ | Auto-increment Large ID |
💡 แนะนำ: ใช้ IDENTITY แทน SERIAL
PostgreSQL 10+ แนะนำให้ใช้ GENERATED ALWAYS AS IDENTITY แทน SERIAL เพราะเป็น SQL Standard และควบคุมได้ดีกว่า
// Character Types
ประเภทข้อความ
| Type | Storage | ความแตกต่าง | การใช้งาน |
|---|---|---|---|
CHAR(n) | Fixed n bytes | Pad ด้วย spaces | Codes ที่มีความยาวคงที่ เช่น รหัสประเทศ |
VARCHAR(n) | Variable ≤ n | ไม่ Pad spaces | Names, Emails, Short Text ที่มี Max Length |
TEXT | Variable unlimited | ไม่จำกัดความยาว | Long Text, Articles, Descriptions — แนะนำ! |
💡 TEXT คือตัวเลือกที่ดีที่สุดใน PostgreSQL
PostgreSQL เก็บ TEXT และ VARCHAR อย่างมีประสิทธิภาพเหมือนกัน แต่ TEXT ง่ายกว่าในการใช้งาน ไม่ต้องกังวลเรื่อง Max Length
// Boolean
Boolean Type
| Type | Values | Storage |
|---|---|---|
BOOLEAN | TRUE / FALSE / NULL | 1 byte |
-- PostgreSQL รับหลายรูปแบบ
TRUE = 'true', 'yes', 'on', '1'
FALSE = 'false', 'no', 'off', '0'
// Date & Time
วันที่และเวลา
| Type | Storage | Range |
|---|---|---|
DATE | 4 bytes | 4713 BC ถึง 5874897 AD |
TIME | 8 bytes | 00:00:00 ถึง 24:00:00 |
TIMESTAMP | 8 bytes | 4713 BC ถึง 294276 AD |
TIMESTAMPTZ | 8 bytes | Timestamp + Timezone |
INTERVAL | 16 bytes | Duration / Period |
// JSON Types — PostgreSQL Exclusive
JSON & JSONB — จุดเด่นของ PostgreSQL
| Type | Storage Format | ข้อดี | ข้อเสีย | แนะนำใช้เมื่อ |
|---|---|---|---|---|
JSON | Text (ตามที่ Input) | รักษา Whitespace, Key Order | Query ช้า ไม่มี Index | เก็บ Raw JSON ต้องการ Exact Replay |
JSONB | Binary (Parsed) | Index ได้ (GIN), Query เร็ว | เขียนช้ากว่าเล็กน้อย | แนะนำ! ใช้ทั่วไป Query/Filter |
-- สร้าง Table ด้วย JSONB
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT,
attrs JSONB
);
-- Index สำหรับ JSONB (GIN)
CREATE INDEX idx_attrs ON products USING GIN(attrs);
-- Query JSONB
SELECT * FROM products
WHERE attrs @> '{"color": "blue"}';
-- ดึงค่าจาก JSONB
SELECT attrs->'color' AS color FROM products;
// Array Type — PostgreSQL Exclusive
Array Type
PostgreSQL รองรับ Array ของทุก Data Type — ไม่ต้องสร้าง Relation Table สำหรับข้อมูลง่ายๆ
-- Array Column
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title TEXT,
tags TEXT[]
);
-- Insert Array
INSERT INTO articles (title, tags)
VALUES ('PostgreSQL Guide', ARRAY['database','postgres','guide']);
-- Query ด้วย ANY
SELECT * FROM articles
WHERE 'postgres' = ANY(tags);
-- Query ด้วย @> (contains)
SELECT * FROM articles
WHERE tags @> ARRAY['postgres'];
// Special Types
Special & Advanced Types
| Type | ประเภท | การใช้งาน |
|---|---|---|
UUID | Unique Identifier | Global Unique ID (128-bit), ดีกว่า SERIAL สำหรับ Distributed Systems |
INET | Network Address | IP Address (IPv4/IPv6) พร้อม Network Operations |
CIDR | Network Range | IP Network Range เช่น 192.168.0.0/24 |
MACADDR | MAC Address | Hardware MAC Address |
POINT | Geometric | จุดพิกัด (x,y) บน Plane |
POLYGON | Geometric | รูปหลายเหลี่ยม |
TSVECTOR | Full-Text Search | Preprocessed Text สำหรับ Full-Text Search |
TSQUERY | Full-Text Search | Search Query สำหรับ Full-Text Search |
BYTEA | Binary | Binary Data (images, files) — แบบ BLOB |
XML | XML Document | เก็บและ Query ข้อมูล XML |
INT4RANGE | Range | ช่วงของ Integer เช่น [1, 100) |
TSTZRANGE | Range | ช่วงเวลาพร้อม Timezone |
💡 PostgreSQL vs MySQL: Data Types
PostgreSQL มี Built-in Types มากกว่า MySQL อย่างมาก โดยเฉพาะ JSONB, Array, UUID, Range Types, Network Address Types และ Geometric Types ที่ MySQL ไม่มีหรือต้องใช้ Extension