---
title: "Discussion on \"Implementing ULIDs in Ruby on Rails with PostgreSQL\" | Hashnode"
description: "Discussion on \"Implementing ULIDs in Ruby on Rails with PostgreSQL\". A Practical Guide to Implementing ULIDs in Ruby on Rails Applications using a PostgreSQL function"
url: "https://hashnode.com/posts/implementing-ulids-in-ruby-on-rails-with-postgresql/65e4e52e881e844aa8696e3c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Implementing ULIDs in Ruby on Rails with PostgreSQL](https://dev.rodloboz.com/implementing-ulids-in-ruby-on-rails-with-postgresql?utm_source=hashnode&utm_medium=feed)

A Practical Guide to Implementing ULIDs in Ruby on Rails Applications using a PostgreSQL function

Mar 3, 2024 · 8 min read · dev.rodloboz.com  
Updated Mar 5, 2024  
Read the article

-   [#rails](https://hashnode.com/tag/rails)
-   [#postgresql](https://hashnode.com/tag/postgresql)
-   [#ulid](https://hashnode.com/tag/ulid)

## Written by

[Rui Freitas](https://hashnode.com/@rodloboz)

@rodloboz · Lisbon, Portugal

  
[View profile](https://hashnode.com/@rodloboz)

Software Engineer. I work mostly with Ruby, Python, Elixir and JavaScript.

## Responses

Markdown supported · ⌘↵ to post

[Ishaq Bhallil](https://hashnode.com/@isbhallil)

[Feb 14, 2025](https://hashnode.com/posts/implementing-ulids-in-ruby-on-rails-with-postgresql/65e4e52e881e844aa8696e3c/comment/67af84e3c41bea653aa89588)

add this forget about it

#config/initializers/ulid\_primary\_key\_defaults.rb module ActiveRecord module ConnectionAdapters class TableDefinition alias\_method :original\_primary\_key, :primary\_key

def primary\_key(name, type = :bigint, **options) if type == :ulid && !options.key?(:default) options\[:default\] = -> { "gen\_ulid()" } end original\_primary\_key(name, type,** options) end end end end

[Ishaq Bhallil](https://hashnode.com/@isbhallil)

[Feb 14, 2025](https://hashnode.com/posts/implementing-ulids-in-ruby-on-rails-with-postgresql/65e4e52e881e844aa8696e3c/comment/67af84e3c41bea653aa89588#reply-67af85239bfa1654b6e21e51)

here is the migration after "

create\_table :users, id: :ulid do |t| t.string :name t.timestamps end

"
