unit
SI units extension
Repository
df7cb/postgresql-unit
https://github.com/df7cb/postgresql-unit
Source
postgresql-unit-7.10.tar.gz
postgresql-unit-7.10.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pgunit | 7.10 | TYPE | GPL-2.0 | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3520 | unit | No | Yes | No | Yes | No | No | - |
| Related | plpgsql pgmp numeral prefix semver pgpdf pglite_fusion md5hash asn1oid |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 7.10 | 1817161514 | pgunit | plpgsql |
| RPM | PGDG | 7.10 | 1817161514 | postgresql-unit_$v | - |
| DEB | PGDG | 7.10 | 1817161514 | postgresql-$v-unit | - |
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
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
| Operator | Description | Example |
|---|---|---|
+, - | 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 unitsunit.time_output_custom: Format times using minutes/hours/daysunit.output_superscript: Unicode superscript exponents
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.