> This page location: PostgreSQL Data Types > User-defined Data Types
> Full Neon documentation index: https://neon.com/docs/llms.txt

# A Look at PostgreSQL User-defined Data Types

**Summary**: in this tutorial, you will learn how to create PostgreSQL user-defined data type using `CREATE DOMAIN` and `CREATE TYPE` statements.

Besides built-in [data types](https://neon.com/postgresql/postgresql-tutorial/postgresql-data-types), PostgreSQL allows you to create user-defined data types through the following statements:

- `CREATE DOMAIN` creates a user-defined data type with constraints such as [`NOT NULL`](https://neon.com/postgresql/postgresql-tutorial/postgresql-not-null-constraint), [`CHECK`](https://neon.com/postgresql/postgresql-tutorial/postgresql-check-constraint), etc.
- `CREATE TYPE` creates a composite type used in [stored procedures](../postgresql-plpgsql/postgresql-create-procedure) as the data types of returned values.

## PostgreSQL CREATE DOMAIN statement

In PostgreSQL, a domain is a data type with optional constraints e.g., [`NOT NULL`](https://neon.com/postgresql/postgresql-tutorial/postgresql-not-null-constraint) and [`CHECK`](https://neon.com/postgresql/postgresql-tutorial/postgresql-check-constraint). A domain has a unique name within the schema scope.

Domains are useful for centralizing the management of fields with common constraints. For example, some tables may have the same column that do not accept NULL and spaces.

The following statement [create a table](https://neon.com/postgresql/postgresql-tutorial/postgresql-create-table) named `mailing_list`:

```sql
CREATE TABLE mailing_list (
    id SERIAL PRIMARY KEY,
    first_name VARCHAR NOT NULL,
    last_name VARCHAR NOT NULL,
    email VARCHAR NOT NULL,
    CHECK (
        first_name !~ '\s'
        AND last_name !~ '\s'
    )
);
```

In this table, both `first_name` and `last_name` columns do not accept null and spaces. Instead of defining the `CHECK` constraint, you can create a `contact_name` domain and reuse it in multiple columns.

The following statement uses the `CREATE DOMAIN` to create a new domain called `contact_name` with the `VARCHAR` datatype and do not accept NULL and spaces:

```sql
CREATE DOMAIN contact_name AS
   VARCHAR NOT NULL CHECK (value !~ '\s');
```

And you use `contact_name` as the datatype of the `first_name` and `last_name` columns as a regular built-in type:

```sql
CREATE TABLE mailing_list (
    id serial PRIMARY KEY,
    first_name contact_name,
    last_name contact_name,
    email VARCHAR NOT NULL
);
```

The following statement inserts a new row into the `mailing_list` table:

```sql
INSERT INTO mailing_list (first_name, last_name, email)
VALUES('Jame V','Doe','james.doe@example.com');
```

PostgreSQL issued the following error because the first name contains a space:

```
ERROR:  value for domain contact_name violates check constraint "contact_name_check"

```

The following statement works because it does not violate any constraints of the `contact_name` type:

```sql
INSERT INTO mailing_list (first_name, last_name, email)
VALUES('Jane','Doe','jane.doe@example.com');
```

To change or remove a domain, you use the `ALTER DOMAIN` or `DROP DOMAIN` respectively.

To view all domains in the current database, you use the `\dD` command as follows:

```
test=#\dD
                                     List of domains
 Schema |     Name     |       Type        | Modifier |               Check
--------+--------------+-------------------+----------+-----------------------------------
 public | contact_name | character varying | not null | CHECK (VALUE::text !~ '\s'::text)
(1 row)

```

### Getting domain information

To get all domains in a specific schema, you use the following query:

```sql
SELECT typname
FROM pg_catalog.pg_type
  JOIN pg_catalog.pg_namespace
  	ON pg_namespace.oid = pg_type.typnamespace
WHERE
	typtype = 'd' and nspname = '<schema_name>';
```

The following statement returns domains in the `public` schema of the current database:

```sql
SELECT typname
FROM pg_catalog.pg_type
  JOIN pg_catalog.pg_namespace
  	ON pg_namespace.oid = pg_type.typnamespace
WHERE
	typtype = 'd' and nspname = 'public';
```

![](https://neon.com/postgresqltutorial/PostgreSQL-User-defined-Type-Example.png)

## PostgreSQL CREATE TYPE

The `CREATE TYPE` statement allows you to create a composite type, which can be used as the return type of a function.

Suppose you want to have a function that returns several values: `film_id`, `title`, and `release_year`. The first step is to create a type e.g., `film_summary` as follows:

```sql
CREATE TYPE film_summary AS (
    film_id INT,
    title VARCHAR,
    release_year SMALLINT
);

```

Second, use the `film_summary` data type as the return type of a function:

```sql
CREATE OR REPLACE FUNCTION get_film_summary (f_id INT)
    RETURNS film_summary AS
$$
SELECT
    film_id,
    title,
    release_year
FROM
    film
WHERE
    film_id = f_id ;
$$
LANGUAGE SQL;
```

Third, call the `get_film_summary()` function:

```sql
SELECT * FROM get_film_summary (40);
```

![PostgreSQL user-defined type example](https://neon.com/postgresqltutorial/postgresql-user-defined-type.png)
To change a user-defined type, you use the `ALTER TYPE` statement. To remove a user-defined type, you use the `DROP TYPE` statement.

If you use the `psql` program, you can list all user-defined types in the current database using the `\dT` or `\dT+` command:

```
dvdrental=# \dT
         List of data types
 Schema |     Name     | Description
--------+--------------+-------------
 public | contact_name |
 public | film_summary |
 public | mpaa_rating  |
(3 rows)
```

In this tutorial, you have learned how to create PostgreSQL user-defined types using the `CREATE DOMAIN` and `CREATE TYPE` statements.

---

## Related docs (PostgreSQL Data Types)

- [Boolean](https://neon.com/postgresql/postgresql-tutorial/postgresql-boolean)
- [CHAR, VARCHAR, and TEXT](https://neon.com/postgresql/postgresql-tutorial/postgresql-char-varchar-text)
- [NUMERIC](https://neon.com/postgresql/postgresql-tutorial/postgresql-numeric)
- [DOUBLE PRECISION](https://neon.com/postgresql/postgresql-tutorial/postgresql-double-precision-type)
- [REAL](https://neon.com/postgresql/postgresql-tutorial/postgresql-real-data-type)
- [Integer](https://neon.com/postgresql/postgresql-tutorial/postgresql-integer)
- [SERIAL](https://neon.com/postgresql/postgresql-tutorial/postgresql-serial)
- [DATE](https://neon.com/postgresql/postgresql-tutorial/postgresql-date)
- [TIMESTAMP](https://neon.com/postgresql/postgresql-tutorial/postgresql-timestamp)
- [Interval](https://neon.com/postgresql/postgresql-tutorial/postgresql-interval)
- [TIME](https://neon.com/postgresql/postgresql-tutorial/postgresql-time)
- [UUID](https://neon.com/postgresql/postgresql-tutorial/postgresql-uuid)
- [JSON](https://neon.com/postgresql/postgresql-tutorial/postgresql-json)
- [HSTORE](https://neon.com/postgresql/postgresql-tutorial/postgresql-hstore)
- [Array](https://neon.com/postgresql/postgresql-tutorial/postgresql-array)
- [Enum](https://neon.com/postgresql/postgresql-tutorial/postgresql-enum)
- [XML](https://neon.com/postgresql/postgresql-tutorial/postgresql-xml-data-type)
- [BYTEA](https://neon.com/postgresql/postgresql-tutorial/postgresql-bytea-data-type)
- [Composite Types](https://neon.com/postgresql/postgresql-tutorial/postgresql-composite-types)
