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 Type | Java Type | Notes |
|---|---|---|
int2 / smallint | Short | 16-bit signed integer |
int4 / integer | Integer | 32-bit signed integer |
int8 / bigint | Long | 64-bit signed integer |
float4 / real | Float | 32-bit IEEE 754 |
float8 / double precision | Double | 64-bit IEEE 754 |
numeric / decimal | BigDecimal | Arbitrary precision |
money | Money | Currency with 2 decimal places |
- Kotlin
- Java
- Scala
val intType: PgType<Int> = PgTypes.int4
val decimalType: PgType<BigDecimal> = PgTypes.numeric
val moneyType: PgType<Money> = PgTypes.money
PgType<Integer> intType = PgTypes.int4;
PgType<BigDecimal> decimalType = PgTypes.numeric;
PgType<Money> moneyType = PgTypes.money;
val intType: PgType[Int] = PgTypes.int4
val decimalType: PgType[BigDecimal] = PgTypes.numeric
val moneyType: PgType[Money] = PgTypes.money
Boolean Type
| PostgreSQL Type | Java Type |
|---|---|
bool / boolean | Boolean |
- Kotlin
- Java
- Scala
val boolType: PgType<Boolean> = PgTypes.bool
PgType<Boolean> boolType = PgTypes.bool;
val boolType: PgType[Boolean] = PgTypes.bool
String Types
| PostgreSQL Type | Java Type | Notes |
|---|---|---|
text | String | Variable unlimited length |
varchar(n) | String | Variable length with limit |
bpchar / char(n) | String | Fixed-length, blank-padded |
name | String | 63-character identifier |
- Kotlin
- Java
- Scala
val textType: PgType<String> = PgTypes.text
val charType: PgType<String> = PgTypes.bpchar(10) // char(10)
PgType<String> textType = PgTypes.text;
PgType<String> charType = PgTypes.bpchar(10); // char(10)
val textType: PgType[String] = PgTypes.text
val charType: PgType[String] = PgTypes.bpchar(10) // char(10)
Binary Types
| PostgreSQL Type | Java Type | Notes |
|---|---|---|
bytea | byte[] | Variable-length binary |
- Kotlin
- Java
- Scala
val bytesType: PgType<ByteArray> = PgTypes.bytea
PgType<byte[]> bytesType = PgTypes.bytea;
val bytesType: PgType[Array[Byte]] = PgTypes.bytea
Date/Time Types
| PostgreSQL Type | Java Type | Notes |
|---|---|---|
date | LocalDate | Date without time |
time | LocalTime | Time without timezone |
timetz | OffsetTime | Time with timezone |
timestamp | LocalDateTime | Date and time without timezone |
timestamptz | Instant | Date and time with timezone (stored as UTC) |
interval | PGInterval | Time duration |
- Kotlin
- Java
- Scala
val dateType: PgType<LocalDate> = PgTypes.date
val timestamptzType: PgType<Instant> = PgTypes.timestamptz
val intervalType: PgType<PGInterval> = PgTypes.interval
PgType<LocalDate> dateType = PgTypes.date;
PgType<Instant> timestamptzType = PgTypes.timestamptz;
PgType<PGInterval> intervalType = PgTypes.interval;
val dateType: PgType[LocalDate] = PgTypes.date
val timestamptzType: PgType[Instant] = PgTypes.timestamptz
val intervalType: PgType[PGInterval] = PgTypes.interval
UUID Type
| PostgreSQL Type | Java Type |
|---|---|
uuid | java.util.UUID |
- Kotlin
- Java
- Scala
val uuidType: PgType<UUID> = PgTypes.uuid
PgType<UUID> uuidType = PgTypes.uuid;
val uuidType: PgType[UUID] = PgTypes.uuid
JSON Types
| PostgreSQL Type | Java Type | Notes |
|---|---|---|
json | Json | Stored as-is, validated on input |
jsonb | Jsonb | Binary format, indexed, normalized |
- Kotlin
- Java
- Scala
val jsonType: PgType<Json> = PgTypes.json
val jsonbType: PgType<Jsonb> = PgTypes.jsonb
// Parse and use JSON
val data: Json = Json("{\"name\": \"John\"}")
PgType<Json> jsonType = PgTypes.json;
PgType<Jsonb> jsonbType = PgTypes.jsonb;
// Parse and use JSON
Json data = new Json("{\"name\": \"John\"}");
val jsonType: PgType[Json] = PgTypes.json
val jsonbType: PgType[Jsonb] = PgTypes.jsonb
// Parse and use JSON
val data: Json = 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 Type | Java Type (Boxed) | Java Type (Unboxed) |
|---|---|---|
int4[] | Integer[] | int[] |
int8[] | Long[] | long[] |
float4[] | Float[] | float[] |
float8[] | Double[] | double[] |
bool[] | Boolean[] | boolean[] |
text[] | String[] | - |
uuid[] | UUID[] | - |
- Kotlin
- Java
- Scala
// Boxed arrays
val intArrayBoxed: PgType<Array<Int>> = PgTypes.int4Array
// Unboxed arrays (more efficient)
val intArrayUnboxed: PgType<IntArray> = PgTypes.int4ArrayUnboxed
// Text arrays
val textArray: PgType<Array<String>> = PgTypes.textArray
// Any type can be made into an array
val uuidArray: PgType<Array<UUID>> = PgTypes.uuidArray
// 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;
// Boxed arrays
val intArrayBoxed: PgType[Array[Int]] = PgTypes.int4Array
// Unboxed arrays (more efficient)
val intArrayUnboxed: PgType[Array[Int]] = PgTypes.int4ArrayUnboxed
// Text arrays
val textArray: PgType[Array[String]] = PgTypes.textArray
// Any type can be made into an array
val uuidArray: PgType[Array[UUID]] = PgTypes.uuidArray
Range Types
PostgreSQL's range types represent intervals of values with inclusive/exclusive bounds:
| PostgreSQL Type | Java Type | Element Type |
|---|---|---|
int4range | Range<Integer> | Integer |
int8range | Range<Long> | Long |
numrange | Range<BigDecimal> | BigDecimal |
daterange | Range<LocalDate> | LocalDate |
tsrange | Range<LocalDateTime> | LocalDateTime |
tstzrange | Range<Instant> | Instant |
- Kotlin
- Java
- Scala
val intRangeType: PgType<Range<Int>> = PgTypes.int4range
val dateRangeType: PgType<Range<LocalDate>> = PgTypes.daterange
// Create ranges with explicit bounds
val range: Range<Int> = Range.int4(
RangeBoundClosed(1),
RangeBoundClosed(10)
) // [1, 11) after normalization
// Check containment
val contains: Boolean = range.contains(5) // true
PgType<Range<Integer>> intRangeType = PgTypes.int4range;
PgType<Range<LocalDate>> dateRangeType = PgTypes.daterange;
// Create ranges with explicit bounds
Range<Integer> range = Range.int4(
new RangeBound.Closed<>(1),
new RangeBound.Closed<>(10)
); // [1, 11) after normalization
// Check containment
boolean contains = range.contains(5); // true
val intRangeType: PgType[Range[Integer]] = PgTypes.int4range
val dateRangeType: PgType[Range[LocalDate]] = PgTypes.daterange
// Create ranges with explicit bounds
val range: Range[Integer] = Range.int4(
new RangeBound.Closed[Integer](1),
new RangeBound.Closed[Integer](10)
) // [1, 11) after normalization
// Check containment
val contains: Boolean = range.contains(5) // true
Geometric Types
PostgreSQL's geometric types for 2D shapes:
| PostgreSQL Type | Java Type | Description |
|---|---|---|
point | PGpoint | (x, y) coordinate |
line | PGline | Infinite line |
lseg | PGlseg | Line segment |
box | PGbox | Rectangular box |
path | PGpath | Open or closed path |
polygon | PGpolygon | Closed polygon |
circle | PGcircle | Circle with center and radius |
- Kotlin
- Java
- Scala
val pointType: PgType<PGpoint> = PgTypes.point
val circleType: PgType<PGcircle> = PgTypes.circle
val polygonType: PgType<PGpolygon> = PgTypes.polygon
// Create geometric objects
val point: PGpoint = PGpoint(1.0, 2.0)
val circle: PGcircle = PGcircle(point, 5.0)
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);
val pointType: PgType[PGpoint] = PgTypes.point
val circleType: PgType[PGcircle] = PgTypes.circle
val polygonType: PgType[PGpolygon] = PgTypes.polygon
// Create geometric objects
val point: PGpoint = new PGpoint(1.0, 2.0)
val circle: PGcircle = new PGcircle(point, 5.0)
Network Types
Types for storing network addresses:
| PostgreSQL Type | Java Type | Description |
|---|---|---|
inet | Inet | IPv4 or IPv6 host address |
cidr | Cidr | IPv4 or IPv6 network |
macaddr | Macaddr | MAC address (6 bytes) |
macaddr8 | Macaddr8 | MAC address (8 bytes, EUI-64) |
- Kotlin
- Java
- Scala
val inetType: PgType<Inet> = PgTypes.inet
val cidrType: PgType<Cidr> = PgTypes.cidr
val addr: Inet = Inet("192.168.1.1/24")
PgType<Inet> inetType = PgTypes.inet;
PgType<Cidr> cidrType = PgTypes.cidr;
Inet addr = new Inet("192.168.1.1/24");
val inetType: PgType[Inet] = PgTypes.inet
val cidrType: PgType[Cidr] = PgTypes.cidr
val addr: Inet = new Inet("192.168.1.1/24")
Text Search Types
Full-text search types:
| PostgreSQL Type | Java Type | Description |
|---|---|---|
tsvector | Tsvector | Text search document |
tsquery | Tsquery | Text search query |
- Kotlin
- Java
- Scala
// Text search types are available via PgTypes
// Note: tsvector and tsquery have specialized handling
val textType: PgType<String> = PgTypes.text
// Text search types are available via PgTypes
// Note: tsvector and tsquery have specialized handling
PgType<String> textType = PgTypes.text;
// Text search types are available via PgTypes
// Note: tsvector and tsquery have specialized handling
val textType: PgType[String] = PgTypes.text
XML Type
| PostgreSQL Type | Java Type |
|---|---|
xml | Xml |
- Kotlin
- Java
- Scala
val xmlType: PgType<Xml> = PgTypes.xml
val doc: Xml = Xml("<root><child>text</child></root>")
PgType<Xml> xmlType = PgTypes.xml;
Xml doc = new Xml("<root><child>text</child></root>");
val xmlType: PgType[Xml] = PgTypes.xml
val doc: Xml = new Xml("<root><child>text</child></root>")
Other Special Types
| PostgreSQL Type | Java Type | Description |
|---|---|---|
hstore | Map<String, String> | Key-value store |
vector | Vector | pgvector extension |
record | Record | Anonymous composite type |
- Kotlin
- Java
- Scala
val hstoreType: PgType<Map<String, String>> = PgTypes.hstore
val vectorType: PgType<Vector> = PgTypes.vector
PgType<Map<String, String>> hstoreType = PgTypes.hstore;
PgType<Vector> vectorType = PgTypes.vector;
val hstoreType: PgType[Map[String, String]] = PgTypes.hstore
val vectorType: PgType[Vector] = PgTypes.vector
System Types
Types used internally by PostgreSQL:
| PostgreSQL Type | Java Type | Description |
|---|---|---|
oid | Long | Object identifier |
xid | Xid | Transaction ID |
regclass | Regclass | Relation name/OID |
regtype | Regtype | Type name/OID |
regproc | Regproc | Function name/OID |
Enum Types
PostgreSQL enums are mapped to Java enums:
- Kotlin
- Java
- Scala
// Define your Kotlin enum
enum class Status { PENDING, ACTIVE, COMPLETED }
// Create a PgType for it
val statusType: PgType<Status> = PgTypes.ofEnum("status", Status::valueOf)
// Define your Java enum
public enum Status { PENDING, ACTIVE, COMPLETED }
// Create a PgType for it
PgType<Status> statusType = PgTypes.ofEnum("status", Status::valueOf);
// Define your Scala 3 enum
enum Status:
case PENDING, ACTIVE, COMPLETED
// Create a PgType using transform (Scala 3 enums don't extend java.lang.Enum)
val statusType: PgType[Status] = PgTypes.text.transform(
Status.valueOf,
_.toString
)
Custom Domain Types
Wrap base types with custom Java types using transform:
- Kotlin
- Java
- Scala
// Wrapper type
data class Email(val value: String)
// Create PgType from text
val emailType: PgType<Email> = PgTypes.text.transform(::Email, Email::value)
// Wrapper type
public record Email(String value) {}
// Create PgType from text
PgType<Email> emailType = PgTypes.text.transform(Email::new, Email::value);
// Wrapper type
case class Email(value: String)
// Create PgType from text
val emailType: PgType[Email] = PgTypes.text.transform(Email.apply, _.value)
Nullable Types
Any type can be made nullable using .opt():
- Kotlin
- Java
- Scala
val notNull: PgType<Int> = PgTypes.int4
val nullable: PgType<Int?> = PgTypes.int4.opt()
PgType<Integer> notNull = PgTypes.int4;
PgType<Optional<Integer>> nullable = PgTypes.int4.opt(); // null values allowed
val notNull: PgType[Int] = PgTypes.int4
val nullable: PgType[Option[Int]] = PgTypes.int4.opt