Skip to content

Data Types

PetraDB follows PostgreSQL conventions for SQL syntax, identifier handling, and type casting.

  • Case-insensitive keywordsSELECT, select, and Select are equivalent
  • Unquoted identifier folding — unquoted identifiers fold to lowercase (CREATE TABLE Users → table name users)
  • Double-quoted identifiers — preserve case ("MixedCase" stays as-is)
  • String escaping — doubled single quotes ('it''s') and E-strings (E'it\'s')
  • Operators — both != and <> for not-equal
TypeDescription
SMALLINT16-bit integer (-32768 to 32767)
INT / INTEGER32-bit integer
BIGINT64-bit integer
SMALLSERIALAuto-incrementing 16-bit integer (creates backing sequence)
SERIALAuto-incrementing 32-bit integer (creates backing sequence)
BIGSERIALAuto-incrementing 64-bit integer (creates backing sequence)
DOUBLE / FLOAT / REALDouble-precision floating point
NUMERIC(p,s) / DECIMAL(p,s)Fixed-precision decimal
TEXTVariable-length string
CHAR(n)Fixed-length string (right-padded with spaces)
VARCHAR(n)Variable-length string (max n characters, no padding)
BOOLEANTrue/false
DATECalendar date (yyyy-MM-dd)
TIMETime of day (HH:mm:ss)
TIMESTAMPDate and time
TIMESTAMP WITH TIME ZONEDate and time with timezone offset
INTERVALDuration (ISO 8601 or N days N hours N minutes N seconds)
UUIDUniversally unique identifier
JSON / JSONBStructured JSON objects and arrays
BYTEABinary data
ENUMCustom enumerated types (via CREATE TYPE ... AS ENUM)
INT[], TEXT[], etc.Typed arrays (any base type with [] suffix)

Use the :: operator or CAST(expr AS type) to convert between types:

SELECT '2024-06-15'::DATE;
SELECT CAST('14:30:00' AS TIME);
SELECT '2 hours 30 minutes'::INTERVAL;
SELECT CAST(val AS TEXT);
SELECT '42'::INT;
SELECT 1::BOOLEAN;
SELECT EXTRACT(year FROM created_at);
SELECT '2024-01-01'::DATE + 10; -- add days
SELECT '2024-01-15'::DATE - '2024-01-10'::DATE; -- days between
SELECT now() + '2 hours'::INTERVAL; -- timestamp + interval
SELECT now() - '30 minutes'::INTERVAL; -- timestamp - interval
SELECT '1 hour'::INTERVAL * 3; -- scale interval
SELECT EXTRACT(year FROM now()); -- extract field
SELECT date_trunc('month', now()); -- truncate
PRIMARY KEY (id)
UNIQUE (email)
NOT NULL
DEFAULT value
FOREIGN KEY (col) REFERENCES other_table (col) ON DELETE CASCADE ON UPDATE CASCADE