Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a PostgreSQL column that is a set of enum values?

I like that I can create new enum types in PostgreSQL. But what if I want a column value that is a set of enum values. Do I need to implement that manually with an integer column type and bitwise operators, or is there a way to keep using the enums by name?

CREATE TYPE foo AS ENUM ('none', 'loud', 'bright', 'cheap')
CREATE TABLE t (
    id serial,
    properties [set of foo?]
)
...
SELECT * FROM t;
1      loud
2      loud, cheap
3      bright
4      none
...
like image 569
Rob N Avatar asked Nov 26 '25 18:11

Rob N


1 Answers

You can use an array:

CREATE TYPE foo AS ENUM ('none', 'loud', 'bright', 'cheap');
CREATE TABLE t (
    id serial,
    properties foo[]
);
like image 104
Flimzy Avatar answered Nov 28 '25 17:11

Flimzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!