20

Question:

In PostgreSQL (using SQL, not the console), how can I list all available datataypes ?

Ideally like this: http://www.java2s.com/Code/PostgreSQL/Postgre-SQL/Displaysalldatatypesintheconnecteddatabasewithcomments.htm

It should also list user defined types, if there are any.
Just like the list in pgAdmin3 where you define the datatype for a new column in a table.

1

2 Answers 2

35

"data types" in PostgreSQL actually includes primitive (built-in) types, types added by extensions, user-defined composite types, domains, and table rowtypes. It isn't clear which of these are of interest to you. All types available in a given database are listed in that database's pg_catalog.pg_type so you may need to filter the results. See the documentation for the pg_type system catalog table.

Types for available but not installed extensions are not listed. There's no way to list types provided by extensions not installed in the current database.

To get a prettier listing of types use psql's \dT * command. You can see the underlying SQL this executes by running psql with the -E flag:

$ psql -E regress
regress=> \dT *
********* QUERY **********
SELECT n.nspname as "Schema",
  pg_catalog.format_type(t.oid, NULL) AS "Name",
  pg_catalog.obj_description(t.oid, 'pg_type') as "Description"
FROM pg_catalog.pg_type t
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
  AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
  AND pg_catalog.pg_type_is_visible(t.oid)
ORDER BY 1, 2;
**************************
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know if the OIDs for built-in types are consistent across server instances and versions? E.g., will the date type always be 1082?
@ps2goat For built-in non-extension types, yes, the oids are consistent but you should not rely on this in anything you distribute, expect to be portable, etc. It's not guaranteed to remain the case.
18
select * from pg_type;

pg_type

1 Comment

do you want to list all types in your particular db, or all datatypes that are avaiable in Postgres?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.