Skip to main content

PostgreSQL Type Support

Foundations JDBC provides comprehensive support for all PostgreSQL data types, including the many exotic types that make PostgreSQL unique.

Numeric Types

PostgreSQL TypeJava TypeNotes
int2 / smallintShort16-bit signed integer
int4 / integerInteger32-bit signed integer
int8 / bigintLong64-bit signed integer
float4 / realFloat32-bit IEEE 754
float8 / double precisionDouble64-bit IEEE 754
numeric / decimalBigDecimalArbitrary precision
moneyMoneyCurrency with 2 decimal places
PgType<Integer> intType = PgTypes.int4;
PgType<BigDecimal> decimalType = PgTypes.numeric;
PgType<Money> moneyType = PgTypes.money;

Boolean Type

PostgreSQL TypeJava Type
bool / booleanBoolean
PgType<Boolean> boolType = PgTypes.bool;

String Types

PostgreSQL TypeJava TypeNotes
textStringVariable unlimited length
varchar(n)StringVariable length with limit
bpchar / char(n)StringFixed-length, blank-padded
nameString63-character identifier
PgType<String> textType = PgTypes.text;
PgType<String> charType = PgTypes.bpchar(10); // char(10)

Binary Types

PostgreSQL TypeJava TypeNotes
byteabyte[]Variable-length binary
PgType<byte[]> bytesType = PgTypes.bytea;

Date/Time Types

PostgreSQL TypeJava TypeNotes
dateLocalDateDate without time
timeLocalTimeTime without timezone
timetzOffsetTimeTime with timezone
timestampLocalDateTimeDate and time without timezone
timestamptzInstantDate and time with timezone (stored as UTC)
intervalPGIntervalTime duration
PgType<LocalDate> dateType = PgTypes.date;
PgType<Instant> timestamptzType = PgTypes.timestamptz;
PgType<PGInterval> intervalType = PgTypes.interval;

UUID Type

PostgreSQL TypeJava Type
uuidjava.util.UUID
PgType<UUID> uuidType = PgTypes.uuid;

JSON Types

PostgreSQL TypeJava TypeNotes
jsonJsonStored as-is, validated on input
jsonbJsonbBinary format, indexed, normalized
PgType<Json> jsonType = PgTypes.json;
PgType<Jsonb> jsonbType = PgTypes.jsonb;

// Parse and use JSON
Json data = new Json("{\"name\": \"John\"}");

Array Types

Any PostgreSQL type can be used as an array. Foundations JDBC supports both boxed and unboxed array variants:

PostgreSQL TypeJava Type (Boxed)Java Type (Unboxed)
int4[]Integer[]int[]
int8[]Long[]long[]
float4[]Float[]float[]
float8[]Double[]double[]
bool[]Boolean[]boolean[]
text[]String[]-
uuid[]UUID[]-
// Boxed arrays
PgType<Integer[]> intArrayBoxed = PgTypes.int4Array;

// Unboxed arrays (more efficient)
PgType<int[]> intArrayUnboxed = PgTypes.int4ArrayUnboxed;

// Text arrays
PgType<String[]> textArray = PgTypes.textArray;

// Any type can be made into an array
PgType<UUID[]> uuidArray = PgTypes.uuidArray;

Range Types

PostgreSQL's range types represent intervals of values with inclusive/exclusive bounds:

PostgreSQL TypeJava TypeElement Type
int4rangeRange<Integer>Integer
int8rangeRange<Long>Long
numrangeRange<BigDecimal>BigDecimal
daterangeRange<LocalDate>LocalDate
tsrangeRange<LocalDateTime>LocalDateTime
tstzrangeRange<Instant>Instant
PgType<Range<Integer>> intRangeType = PgTypes.int4range;
PgType<Range<LocalDate>> dateRangeType = PgTypes.daterange;

// Create ranges
Range<Integer> range = Range.INT4.closed(1, 10); // [1, 10]
Range<Integer> halfOpen = Range.INT4.closedOpen(1, 10); // [1, 10)

// Check containment
boolean contains = range.contains(5); // true

Geometric Types

PostgreSQL's geometric types for 2D shapes:

PostgreSQL TypeJava TypeDescription
pointPGpoint(x, y) coordinate
linePGlineInfinite line
lsegPGlsegLine segment
boxPGboxRectangular box
pathPGpathOpen or closed path
polygonPGpolygonClosed polygon
circlePGcircleCircle with center and radius
PgType<PGpoint> pointType = PgTypes.point;
PgType<PGcircle> circleType = PgTypes.circle;
PgType<PGpolygon> polygonType = PgTypes.polygon;

// Create geometric objects
PGpoint point = new PGpoint(1.0, 2.0);
PGcircle circle = new PGcircle(point, 5.0);

Network Types

Types for storing network addresses:

PostgreSQL TypeJava TypeDescription
inetInetIPv4 or IPv6 host address
cidrCidrIPv4 or IPv6 network
macaddrMacaddrMAC address (6 bytes)
macaddr8Macaddr8MAC address (8 bytes, EUI-64)
PgType<Inet> inetType = PgTypes.inet;
PgType<Cidr> cidrType = PgTypes.cidr;

Inet addr = new Inet("192.168.1.1/24");

Text Search Types

Full-text search types:

PostgreSQL TypeJava TypeDescription
tsvectorTsvectorText search document
tsqueryTsqueryText search query
PgType<Tsvector> vectorType = PgTypes.tsvector;
PgType<Tsquery> queryType = PgTypes.tsquery;

XML Type

PostgreSQL TypeJava Type
xmlXml
PgType<Xml> xmlType = PgTypes.xml;
Xml doc = new Xml("<root><child>text</child></root>");

Other Special Types

PostgreSQL TypeJava TypeDescription
hstoreMap<String, String>Key-value store
vectorVectorpgvector extension
recordRecordAnonymous composite type
PgType<Map<String, String>> hstoreType = PgTypes.hstore;
PgType<Vector> vectorType = PgTypes.vector;

System Types

Types used internally by PostgreSQL:

PostgreSQL TypeJava TypeDescription
oidLongObject identifier
xidXidTransaction ID
regclassRegclassRelation name/OID
regtypeRegtypeType name/OID
regprocRegprocFunction name/OID

Enum Types

PostgreSQL enums are mapped to Java enums:

// Define your Java enum
public enum Status { PENDING, ACTIVE, COMPLETED }

// Create a PgType for it
PgType<Status> statusType = PgTypes.ofEnum("status", Status::valueOf);

Custom Domain Types

Wrap base types with custom Java types using bimap:

// Wrapper type
public record Email(String value) {}

// Create PgType from text
PgType<Email> emailType = PgTypes.text.bimap(Email::new, Email::value);

Nullable Types

Any type can be made nullable using .nullable():

PgType<Integer> notNull = PgTypes.int4;
PgType<Integer> nullable = PgTypes.int4.nullable(); // null values allowed