
In the PostgreSQL database server, an enumerated (enum) type is a custom data type containing a list of permitted input values. You can map the PostgreSQL enum data types to a table using the CREATE TABLE command. Enum data types are suitable in scenarios where you want to move some validation logic to the database level and prevent users from inserting invalid data.
Also, only use the enum data types in a table column that requires a clear set of items (usually around ten) that rarely change (For instance, colors, continents, and product types). Otherwise, lookup tables with correctly linked foreign keys might be more appropriate.
This guide shows you how to use a managed PostgreSQL database cluster to define and use custom enum data types on Ubuntu 20.04.
To test this guide:
Sign in to your Vultr account. Locate Products on the left side menu, then click Databases on the navigation menu. Click the name of your managed PostgreSQL database cluster under the Managed Database Name. Under the Overview tab, locate Connection Details. This guide uses the following sample connection details:
username: vultradmin
password: EXAMPLE_POSTGRESQL_PASSWORD
host: SAMPLE_POSTGRESQL_DB_HOST_STRING.vultrdb.com
port: 16751
Before creating the enum data types, establish an SSH connection to your Linux server and execute the following steps to set up a sample database:
Install the postgresql-client package, a command-line client that allows you to establish a secure connection to your managed PostgreSQL database cluster.
Use the PostgreSQL client (psql) to connect to the managed PostgreSQL cluster. Replace the values after -h, -p, -U with the correct host, port, and username from the managed database cluster.
Output.
Enter the PostgreSQL database cluster's password and press Enter to proceed.
Output.
Run the following SQL command to set up a sample company_db database.
Output.
Connect to the new company_db.
Output.
After setting up the database, proceed to the next step to create enum data types.
In PostgreSQL, you should follow the syntax below to create an enum data type:
The PostgreSQL enum command explained:
SAMPLE_ENUM_DATA_TYPE: This is a unique enum data type name. The name must be descriptive enough to avoid confusion.
SAMPLE_ENUM_VALUES: This is a comma-separated list containing quoted enum values (For instance, 'RED', 'BLUE', 'ORANGE').
After familiarizing yourself with the enum data type syntax, follow the steps below to create a few samples:
Create a product_type_enum data type to mark products as either PHYSICAL (tangible goods) or NON-PHYSICAL (services).
Output.
Create an availability_enum data type to set products' availability. The ONLINE status shows that a product is only available from the company's website. The STOREFRONT status shows that the product is only available for pickups. The BOTH status shows that a product is available both on the website and in the company's physical store.
Output.
Ensure your product_type_enum list is in place.
Output.
Run the following query to verify the availability_enum data type values.
Output.
With the two sample custom enum data types in place, proceed to the next step to create a table.
This step shows you how to create a table using your custom enum data types, inserting data, and querying the table to confirm changes. In PostgreSQL, creating a table that uses some custom enum data types is not complicated. Use the following syntax.
Create a sample products table. Ensure the product_type and availability columns use the product_type_enum and availability_enum data types.
Output.
Insert sample data into the products table.
Output
Query the products table to ensure everything is in place.
Output.
Review the validation policy of the enum data types by trying to enter a record with an invalid product_type value.
Output.
The table structure and records are now in place. Learn to sort and compare data records using the enum data type columns in the next step.
When sorting values by an enum type column, PostgreSQL considers the original order declared when you first defined the enum type values.
Query the products table and use the ...ORDER BY availability clause to see the behavior in action.
Verify the output below. As you can see, PostgreSQL sorts the availability column using the order that you defined when creating the availability_enum data type. That is, PostgreSQL doesn't order the records alphabetically.
Override the sort order behavior by casting the enum column to text when running the ORDER BY clause.
Output.
Run the following query that uses a comparison operator (>) to list all products having a product_type enum type index that appears after the PHYSICAL value in the original product_type enum list.
Output.
After sorting data by enum columns, proceed next to learn how to manage existing enum lists.
The PostgreSQL server provides several commands for managing the enum data types and linked values. Test the commands in the following sections.
In the PostgreSQL server, you can change the name of the enum type in case a need arises by following the steps below:
Find the enum data types applied to your products table by running a describe (\d) table command.
Note the enum type names under the Type column in the following output.
Run the following command to change the enum type name from product_type_enum to type_of_product_enum.
Output.
Review the structure of the products table again to confirm the changes.
You've successfully changed the name of the product_type_enum to type_of_product_enum.
To add a new value to an existing enum data type, use the syntax below.
Add a new DOWNLOAD value to the type_of_product_enum.
Output.
List product_type_enum values to verify the change.
Output.
Specify the sort position when adding new enum type values in an existing list using the BEFORE or AFTER keywords as shown below.
Output.
Verify the changes.
Output.
To rename an existing enum data type value, use the following syntax.
To change the 'DOWNLOAD' value to 'SOFTWARE DOWNLOAD' in the type_of_product_enum data type, run the following command.
Output.
Verify the changes.
Output.
PostgreSQL doesn't allow you to change the sort order or delete enum type values from an existing list. The only workaround is dropping and creating the enum type. For instance, to rearrange the sort order of the availability_enum data type from ONLINE, STOREFRONT, BOTH to BOTH, ONLINE, STOREFRONT, follow the steps below:
Verify the active sort order from the availability_enum data type.
Output.
Rename the active enum data type to a new name. You can use the _old suffix at the end of the enum type name to avoid confusion.
Output.
Recreate a new availability_enum data type with your new order.
Output.
Map the new enum data type to the products table by casting the availability column text values to the new availability_enum data type.
Output.
Delete the old (availability_enum_old) enum data type.
Output.
Query the availability_enum data type to ensure the new sort order reflects your desired output.
Output.
This guide shows you how to use a managed PostgreSQL database server enumerated data types on Ubuntu 20.04. In this guide, you have created a sample database, declared two custom enum data types, and applied them to a products table. You've also run some commands to insert data into the enum columns. Towards the end, you've learned how to add and rearrange the enum data type values.
Check out the following links to learn more about Vultr's managed databases:
How to Use Vultr Managed Databases for PostgreSQL with NodeJS.
How to Implement PostgreSQL Database Transactions with Python on Ubuntu 20.04.
0 Comments
Be the first to comment and share your perspective with the community.