Question

How to generate CUIDs in a PostgreSQL trigger on DigitalOcean?

I opted to use CUID(2)s for various reasons but I’m having trouble to get this to fully work in my PostgreSQL database that I’ve hosted on DigitalOcean.

While it works perfectly when I generate them via Prisma, I can’t find a fully working solution when I need to generate them within the database itself, for instance when using a trigger to generate an audit log for certain tables.

There’s a very useful extension that lets you generate various kinds of IDs but it is not supported and I’m not getting my hopes up that it will be any time soon.

I’ve added this function to my database which can generate them but it recommends using a unique value for every server that I’m unable to set.

This is done via SET plpgsql.cuid_hostid = "XXXXXX"; -- for instance 897892. This exact command will fail because plpgsql is a reserved prefix. Using any other prefix works during a session but will fail as soon as the session is terminated.

I can get it to work by omitting this but I fear that this might cause trouble down the line.

My question is: How do I set a server constant like this that will stay set for as long as the server exists?

Also: Why are CUIDs not treated as equals to BigInt and UUIDs?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel