EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL enum

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL enum

PostgreSQL enum

Definition of PostgreSQL enum

PostgreSQL enum is the data type that was used in PostgreSQL to stored same type of values in column field, we can store same type of values using enum. For defining enum data type we need to create the type of enum first to use it into the table, we can define multiple values in enum type to use the same into the table. If we need same column values in table, same time we have using enum.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below is the syntax of enum in PostgreSQL:

  • Create enum type

Create type name_of_enum_type (value_of_enum_type1, value_of_enum_type2, value_of_enum_type3, …, value_of_enum_typeN);

  • Create table using enum type

Create table name_of_table (name_of_column1 data_type, name_of_column2 enum_type, name_of_column3 data_type, …, name_of_columnN data_type);

  • Insert a value of enum data type column

Insert into name_of_table (name_of_column1, name_of_enum_type_column2, name_of_column3, …, name_of_columnN) values (value1, value_of_enum_type, value2, value3, …., ValueN);

  • Alter enum type

Alter type name_of_enum_type add value ‘value_of_enum_type’ AFTER ‘value_of_enum_type’;

Below is the parameter description syntax of enum type in PostgreSQL.

  • Create type – This is defined as create enum data type using create type in PostgreSQL.
  • Name of enum type –This is define as create enum type and use this in table at the time of table creation. We can use this data type on a column at the time of table creation.
  • Name of table –This is defined as name of table on which column we have defining enum data type.
  • Value of enum type 1 to value of enum type N –This is defined as set of value which was we have used in enum data type. We can define multiple value in enum type, after defining the same we have using this into the table at the time of insertion.
  • Create table – This is defined as create a table by using the enum type, we can define enum type to the column in PostgreSQL.
  • Name of column 1 to name of column N –This is defined as name of column which was we have used at the time of table creation. We have also define enum type to the column.
  • Value 1 to value N –This is defined as column field value which was used at the time of insertion into the table.
  • Alter type –We can alter enum type after creation, we can modify the enum type by using the alter type command. We can add a new value into the enum data set to use the same into the table.
  • Add value –This is defined as add a new value to the enum type by using the alter type command.

How enum works in PostgreSQL?

  • Below is the working of enum type in PostgreSQL.
  • If we have use same type of value into the column, same time we have used enum type on the table column.
  • For using enum type we need to create it first, without creating we cannot use into the table.
  • Below example shows that we need to create enum type first,without creating we cannot use into the table.

Code:

CREATE TABLE enum_info (enum_name text, enum_method enum_type, enum_value text);
CREATE TYPE enum_type AS ENUM ('mail', 'text_sms', 'Phone_no');
CREATE TABLE enum_info (enum_name text, enum_method enum_type, enum_value text);
\d+ enum_info;

PostgreSQL enum 1

  • In first example, we have creating enum type table without creating but it will show the error that enum type is not exist.
  • In second example we have created enum type as enum_type. In third example we have use this on enum_method column, same time this will not issue error because we have created enum type before we have use into the table.
  • After using enum type on table column we cannot use other value which was not contain into the enum type data set.
  • Below example shows that we cannot use other value which was not contain into the enum type data set.

Code:

insert into enum_info values ('ABC', 'Email', 'PQR');
insert into enum_info values ('ABC', 'mail', 'PQR');
select * from enum_info;

PostgreSQL enum 2

  • In first example, we have use-value as email in enum type column value but it will issue error invalid input value of enum because we have used value is not stored into the enum data set.
  • In second example we have use-value as mail it will not issue error because mail value is available into the enum data set.

Examples

Below is the example of enum type in PostgreSQL.

Example #1: Create enum type

Below example shows that create enum type. We have creating enum type name as enum_method_test. Also, we have added value as test1, test2, and test3.

Code:

CREATE TYPE enum_method_test AS ENUM ('test1', 'test2', 'test3');

PostgreSQL enum 3

Example #2: Create a table by using enum type

The below example shows that create a table by using enum type. We have creating table name as enum_info_test. Also, we have added enum type on column name as enum_method.

Code:

CREATE TABLE enum_info_test (enum_name text, enum_method enum_method_test, enum_value text);
\d+ enum_info_test;

PostgreSQL enum 4

Example #3: Insert value of enum data type column

Below example shows that insert value into the enum data type column.

Code:

insert into enum_info_test values ('ABC', 'test1', 'enum_data_type');
insert into enum_info_test values ('PQR', 'test2', 'enum_data_type');
insert into enum_info_test values ('PQR', 'test3', 'enum_data_type');
insert into enum_info_test values ('PQR', 'test4', 'enum_data_type');
select * from enum_info_test;

PostgreSQL enum 5

Example #4: Alter enum type 

Code:

Below example show that alter enum type. We have adding new value as test4 into the enum_method_test type.

Popular Course in this category
Sale
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,206 ratings)
Course Price

View Course

Related Courses

ALTER TYPE enum_method_test ADD VALUE 'test4' AFTER 'test3';

Example 4

Example #5: Drop enum type

Below example shows that drop enum type.

Code:

drop type enum_method_test;

Example 5

Recommended Articles

This is a guide to PostgreSQL enum. Here we discuss the definition, How enum works in PostgreSQL? and examples with code implementation. You may also have a look at the following articles to learn more –

  1. PostgreSQL caching
  2. PostgreSQL Compare Strings
  3. PostgreSQL OID
  4. PostgreSQL group_concat

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More