Back

Why I Built pg_hashids

https://github.com/iCyberon/pg_hashids

When I first needed to generate short, unique, non-sequential IDs inside PostgreSQL, I couldn’t find a good solution that lived entirely within the database. Most approaches required application-level code to generate IDs before inserting rows, which added complexity and round-trips.

The Problem

Sequential IDs leak information. If your user profile is at /users/42, anyone can guess that there are roughly 42 users. URL-safe, non-sequential IDs solve this — but generating them efficiently inside Postgres wasn’t straightforward.

The Solution

pg_hashids is a PostgreSQL extension that brings the Hashids algorithm directly into the database. You can generate short, unique, non-sequential IDs from integers using a simple function call:

SELECT id_encode(42, 'my salt', 8);
-- Returns: 'Kn5rLqYp'

It works as a default value on columns, in triggers, or anywhere you’d use a SQL function.

What I Learned

Building a C extension for PostgreSQL taught me more about Postgres internals than years of using it as a developer. The extension API is powerful but unforgiving — memory management, SPI, and catalog access all require careful handling.

The project has grown to 322 stars on GitHub and is used in production by teams I’ve never met. That’s the best part of open source.