unit

SI units extension

Overview

PackageVersionCategoryLicenseLanguage
pgunit7.10TYPEGPL-2.0C
IDExtensionBinLibLoadCreateTrustRelocSchema
3520unitNoYesNoYesNoNo-
Relatedplpgsql pgmp numeral prefix semver pgpdf pglite_fusion md5hash asn1oid

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG7.101817161514pgunitplpgsql
RPMPGDG7.101817161514postgresql-unit_$v-
DEBPGDG7.101817161514postgresql-$v-unit-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
d12.aarch64
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
d13.x86_64
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
d13.aarch64
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
PGDG 7.10
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

Install

You can install pgunit directly. First, make sure the PGDG repository is added and enabled:

pig repo add pgdg -u          # Add PGDG repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pgunit;          # Install for current active PG version
pig ext install -y pgunit -v 18  # PG 18
pig ext install -y pgunit -v 17  # PG 17
pig ext install -y pgunit -v 16  # PG 16
pig ext install -y pgunit -v 15  # PG 15
pig ext install -y pgunit -v 14  # PG 14
dnf install -y postgresql-unit_18       # PG 18
dnf install -y postgresql-unit_17       # PG 17
dnf install -y postgresql-unit_16       # PG 16
dnf install -y postgresql-unit_15       # PG 15
dnf install -y postgresql-unit_14       # PG 14
apt install -y postgresql-18-unit   # PG 18
apt install -y postgresql-17-unit   # PG 17
apt install -y postgresql-16-unit   # PG 16
apt install -y postgresql-15-unit   # PG 15
apt install -y postgresql-14-unit   # PG 14

Create Extension:

CREATE EXTENSION unit CASCADE;  -- requires: plpgsql

Usage

unit: SI unit data type for PostgreSQL

The unit extension provides a data type for SI units, enabling dimensional analysis and unit conversion directly in SQL.

CREATE EXTENSION unit;

SELECT '9.81 m/s^2'::unit;
SELECT '120 km/h'::unit @ 'm/s' AS velocity;  -- 33.3333333333333 m/s

Base Units

meter (m), kilogram (kg), second (s), ampere (A), kelvin (K), mole (mol), candela (cd), byte (B).

Operators

OperatorDescriptionExample
+, -Add/subtract (same dimensions)'1 m'::unit + '50 cm'::unit
*, /Multiply/divide'5 kg'::unit * '9.81 m/s^2'::unit
^Exponentiation by integer'2 m'::unit ^ 3
@Convert to unit (returns unit)'2 MB/min'::unit @ 'GB/d'
@@Convert to unit (returns double precision)'1 km'::unit @@ 'm'

Functions

Mathematical: sqrt(), exp(), ln(), log2(), cbrt(), asin(), tan(), etc.

Aggregates: sum(unit), avg(unit), min(unit), max(unit), stddev(), variance().

Input Formats

SELECT '3|4 m'::unit;            -- fractions: 0.75 m
SELECT '10:05:30 s'::unit;       -- time format: 36330 s
SELECT 'm⁻²'::unit;              -- Unicode superscripts

Unit Conversion

SELECT '2 MB/min'::unit @ 'GB/d';       -- 2.88 GB/d
SELECT '1 hl'::unit @ '0.5 l';          -- 200 * 0.5 l
SELECT '100 degC'::unit @ 'degF';        -- Fahrenheit conversion

Range Type

SELECT unitrange('earthradius_polar', 'earthradius_equatorial');

Configuration

  • unit.byte_output_iec: Binary prefixes (Ki, Mi, Gi)
  • unit.output_base_units: Show only base units
  • unit.time_output_custom: Format times using minutes/hours/days
  • unit.output_superscript: Unicode superscript exponents

Last Modified 2026-03-12: add pg extension catalog (95749bf)