Skip to content
Short unique id generator for PostgreSQL, using hashids
C Makefile
Branch: master
Clone or download
iCyberon Merge pull request #20 from JaredReisinger/fix-corrupted-returns
Include VARHDRSZ when allocating return value
Latest commit 4a32358 Sep 6, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md Jul 13, 2017
LICENSE
Makefile fixed perms, moved to use palloc0/pfree Jan 13, 2019
README.md fixed perms, moved to use palloc0/pfree Jan 13, 2019
hashids.c moved to ues palloc0/pfree Jan 14, 2019
hashids.h Add ability to encode/decode using custom alphabet Apr 9, 2017
pg_hashids--1.0--1.1.sql Add ability to encode/decode multiple numbers Apr 10, 2017
pg_hashids--1.1--1.2.sql Add ability to encode/decode multiple numbers Apr 10, 2017
pg_hashids--1.2.1.sql Fix issue #14 Jan 12, 2018
pg_hashids--1.2.sql fixed perms, moved to use palloc0/pfree Jan 13, 2019
pg_hashids.c Include VARHDRSZ when allocating return value Sep 6, 2019
pg_hashids.control fixed perms, moved to use palloc0/pfree Jan 13, 2019

README.md

pg_hashids, generate short unique ids from integers

Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It converts numbers like 347 into strings like “yr8”. You can also decode those ids back. This is useful in bundling several parameters into one or simply using them as short UIDs.

You can use hashids to hide primary keys in your database. I've used the extension on several production databases.

Tested PostgreSQL versions : 9.5.X and 9.6.X (Should work on older versions, just not tested)

It's using hashids.c under the hood. More information about hashids and it's implementations here: hashids.org

Installation

Make sure you have development packages installed for postgres and build tools in general.

make; sudo make install

Then in a psql session issue:

CREATE extension pg_hashids;

Update

Install as usual.

make; sudo make install

Then in a psql session issue:

ALTER EXTENSION pg_hashids UPDATE;

or

DROP EXTENSION pg_hashids;
CREATE EXTENSION pg_hashids;

Check

SELECT default_version, installed_version FROM pg_available_extensions WHERE name = 'pg_hashids';

Usage

Encoding

Returns a hash using the default alphabet and empty salt.

SELECT id_encode(1001); -- Result: jNl

Returns a hash using the default alphabet and supplied salt.

SELECT id_encode(1234567, 'This is my salt'); -- Result: Pdzxp

Returns a hash using the default alphabet, salt and minimum hash length.

SELECT id_encode(1234567, 'This is my salt', 10); -- Result: PlRPdzxpR7

Returns a hash using the supplied alphabet, salt and minimum hash length.

SELECT id_encode(1234567, 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890'); -- Result: 3GJ956J9B9

Decoding

You can also decode previouslt generated hashes. Just use the same salt, otherwise you'll get wrong results.

SELECT id_decode('PlRPdzxpR7', 'This is my salt', 10); -- Result: 1234567

Using a custom alphabet

SELECT id_decode('3GJ956J9B9', 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890'); -- Result: 1234567
You can’t perform that action at this time.