Setting up Gender in PostgreSQL

How about using a simple is_male:boolean field?

Or is_female, if you prefer.

Either way, nil (null) should not evaluate to true or false by default.


An enum seems best: http://www.postgresql.org/docs/current/static/datatype-enum.html


This is what domains are for:

DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp;
SET search_path=tmp;

CREATE DOMAIN gender CHAR(1)
    CHECK (value IN ( 'F' , 'M' ) )
    ;

CREATE TABLE persons
    ( pname VARCHAR
    , gend tmp.gender
    );
INSERT INTO persons( pname, gend) VALUES ('Alice', 'F') ,('Bob', 'M') ;
INSERT INTO persons( pname) VALUES ('HAL') ;
INSERT INTO persons( pname, gend) VALUES ('Maurice', 'Z') ;

SELECT * FROM persons;

The output:

DROP SCHEMA
CREATE SCHEMA
SET
CREATE DOMAIN
CREATE TABLE
INSERT 0 2
INSERT 0 1
ERROR:  value for domain gender violates check constraint "gender_check"
 pname | gend 
-------+------
 Alice | F
 Bob   | M
 HAL   | 
(3 rows)

I agree with Mike Jones. ENUM's are perfect for this. ENUM's are awesome for smallish sets of values that rarely change. Gender is a perfect example of this. Usually you only need 5: Male,Female,Intersex,Transgender,Unknown. Tho many go with 3: Male,Female,Other. 2 however (Male,Female) is mean to those that are not male or female.