pg_hashids
pg_hashids
pg_hashids : Short unique id generator for PostgreSQL, using hashids
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4560 | pg_hashids | pg_hashids | 1.3 | FUNC | MIT | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | pg_idkit pg_base58 pgx_ulid pg_uuidv7 sequential_uuids permuteseq |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.3 | 18 17 16 15 14 | pg_hashids | - |
| RPM | PIGSTY | 1.3 | 18 17 16 15 14 | pg_hashids_$v | - |
| DEB | PIGSTY | 1.3 | 18 17 16 15 14 | postgresql-$v-pg-hashids | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
el8.aarch64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
el9.x86_64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
el9.aarch64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
el10.x86_64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
el10.aarch64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
d12.x86_64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
d12.aarch64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
d13.x86_64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
d13.aarch64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
u22.x86_64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
u22.aarch64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
u24.x86_64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
u24.aarch64 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 | PIGSTY 1.3 |
Source
pig build pkg pg_hashids; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pg_hashids; # install via package name, for the active PG version
pig install pg_hashids -v 18; # install for PG 18
pig install pg_hashids -v 17; # install for PG 17
pig install pg_hashids -v 16; # install for PG 16
pig install pg_hashids -v 15; # install for PG 15
pig install pg_hashids -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION pg_hashids;Usage
Converts numbers like 347 into strings like “yr8”. Useful for obfuscating database primary keys.
CREATE EXTENSION pg_hashids;Functions
| Function | Description |
|---|---|
id_encode(number [, salt [, min_length [, alphabet]]]) | Encode an integer to a hashid string |
id_decode(hash, salt, min_length [, alphabet]) | Decode a hashid string back to integer array |
id_decode_once(hash [, salt [, min_length [, alphabet]]]) | Decode a hashid string back to a single integer |
Examples
-- Basic encoding
SELECT id_encode(1001); -- 'jNl'
SELECT id_encode(1234567, 'This is my salt'); -- 'Pdzxp'
SELECT id_encode(1234567, 'This is my salt', 10); -- 'PlRPdzxpR7'
-- Custom alphabet
SELECT id_encode(1234567, 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890');
-- '3GJ956J9B9'
-- Decoding (use the same salt!)
SELECT id_decode('PlRPdzxpR7', 'This is my salt', 10); -- 1234567
SELECT id_decode_once('jNl'); -- 1001
SELECT id_decode_once('Pdzxp', 'This is my salt'); -- 1234567Last updated on