Inspired by the following parent project: ai/nanoid
A tiny, secure, URL-friendly, unique string ID generator for Postgres.
“An amazing level of senseless perfectionism, which is simply impossible not to respect.”
- Small. Just a simple Postgres function.
- Safe. It uses pgcrypto random generator.
- Short IDs. It uses a larger alphabet than UUID (
A-Za-z0-9_-
). So ID size was reduced from 36 to 21 symbols.
SELECT nanoid();
CREATE TABLE mytable (
id char(21) DEFAULT nanoid() PRIMARY KEY
);
or
-- To use a custom id size
CREATE TABLE mytable (
id char(14) DEFAULT nanoid(14) PRIMARY KEY
);
or
-- To use a custom id size and a custom alphabet
CREATE TABLE mytable (
id char(12) DEFAULT nanoid(12, 'ABC123') PRIMARY KEY
);
or
-- To use a custom id size, a custom alphabet and a custom identifier
CREATE TABLE mytable (
id char(12) DEFAULT nanoid(12, 'ABC123', 'usr_') PRIMARY KEY
);
Execute the file nanoid.sql
in order to create on your defined schema the nanoid()
function.
See also the list of contributors who participated in this project. 💕
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.