Understand PostgreSQL Enum type!

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
In this article, you are going to understand the PostgreSQL enum and try to get the answer below question,
PostgreSQL supports the boolean type of binary data type, where you can add values such as yes or no, which is like binary data type because the value of this field is either of one. For example isActive column where you will have either true or false binary values.
If you have more than two values for particular data then you are using the Enum type which is very similar to Java Enum. PostgreSQL support another type i.e. Enum type. Let us take an example type of cricket play format such T-20, 1-Day, and Test Matches. This kind of data type also has to be strict to prevent other values from being written. Then enum comes to the rescue.
An enum type is a top solution to represent the discrete set of values with strict types. Another advantage is that it represents the same analogy that you have in most of the OOP languages such as Java.
It represents the state of the data very well. Let us take an example of the money transfer application where you have multiple states of the transaction, it can be INITIATED, INPROGRESS, WAITING_APPROAVL, APPROVED, WAITBANKCONFIRMATION, and TRANSFERRED. Enum can handle this efficiently.
There may be more or fewer states based on organization and context.
PostgreSQL enum has certain behaviors.
Now you know what is enum, what are the benefits of using enum. Now let us see when to use an enum. Use enum in the below situation:
Setup Docker and Postgres in local click here.
Let us make hands dirty and create an enum by running the below query.
CREATE TYPE product_type AS ENUM
('electronics', 'cosmetic', 'edible');
ALTER TYPE product_type RENAME TO product_type_offline;
SELECT UNNEST(enum_range(null::product_type_offline))
AS product_type;
OR
SELECT unnest(enum_range(NULL::product_type_offline))::text
OR
SELECT enum_range(NULL::product_type_offline)
As your project grows, you want to add a new value to an enum, you can add using the below query.
ALTER TYPE product_type ADD VALUE 'OTHERS';
OR
ALTER TYPE product_type ADD VALUE 'OTHERS' BEFORE 'electronics';
OR
ALTER TYPE product_type ADD VALUE 'OTHERS' AFTER 'cosmetic';
ALTER TYPE product_type RENAME VALUE 'cosmetic' TO 'beauty_products';
DROP TYPE product_type ;
An enum type is a useful feature for data safety and assured the set of values. This article is helpful to remind you of the PostgreSQL enum and Java Enum and how, when, and where to use the enum.
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
==========================**=========================
If this article adds any value for you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.