How to get table definition of a PostgreSQL table?

I suggest you use pg_dump to get tables' and views' definition, it's more convenient. The following command gets the definition of the database francs.

pg_dump -h 127.0.0.1 -E UTF8 -s -v francs > francs.ddl

-s means only dump matching schema, not data.


The simplest way in SQL is to query the information_schema.columns view with a WHERE clause on table_schema and table_name matching yours. All the properties you want (and more) are in the output columns of this single view.

This view is part of the Information Schema whose purpose is to provide standard ways to do database introspection.


All the information used to define each table,column,function etc is stored in the System Catalogs. The contents of these system tables are explained in the manual

Using psql you get easy access to pre-formatted display of this information. Type \? to list available options (under Informational).

If you wish to extract this data to use in your own app then you will probably find looking through the source code of psql an easy way to find examples of selecting the data.

Tags:

Postgresql