Обсуждение: Solved: Getting the field names for a given table

Поиск
Список
Период
Сортировка

Solved: Getting the field names for a given table

От
dcrespo
Дата:
For those who need to know the fields that a certain table has in a
postgresql database, here is the SQL statement:

SELECT DISTINCT attname, relname FROM pg_attribute pa, pg_class pc,
pg_tables pt WHERE pa.attrelid=pc.oid AND pc.relname=pt.tablename AND
pt.schemaname='public' AND attstattarget=-1 AND relname IN (SELECT
tablename FROM pg_tables WHERE schemaname='public') ORDER BY relname;

Notice that you can replace ***relname IN (SELECT tablename FROM
pg_tables WHERE schemaname='public')*** with
***relname='your_desired_tablename'*** and will do the work only for
an specific table. For the newbies: you can specify the schemaname if
needed.

Daniel