PostgreSQL – Best Column Type for ‘Direction’

datatypespostgresql

I want to create a table which has a column named "direction".

This column is either "in" or "out". It is not nullable and it can only contain these two values. Never other values.

Which data type is most feasible?

I use in PostgreSQL. It is 100% ok if the data type is PG specific (only known to this database implementation).

Best Answer

You can create an enumerated type for this:

CREATE TYPE direction AS ENUM ('in', 'out');

CREATE TABLE a_table (
  ID        serial,
  direction direction NOT NULL
);

INSERT INTO a_table (direction) VALUES ('in');        -- succeeds
INSERT INTO a_table (direction) VALUES ('sideways');  -- fails