How do I get the names of all PostgreSQL database tables?

4

How to get the names of all PostgreSQL database tables?

How to get the attributes (code, name, etc ...) of a given table in the PostgreSQL database?

    
asked by anonymous 07.02.2017 / 19:20

2 answers

3

To get the name of all tables, use the command below:

SELECT table_name 
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';

To get the attributes of a table, use the command below:

SELECT column_name, data_type, character_maximum_length
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE table_name = '<NOME DA TABELA>';
    
07.02.2017 / 19:26
1

One of the alternatives is to use the postgres catalogs. The catalog tables have a format more appropriate to postgres as specific types of SGDB and possibility of using some functions.

select
  relnamespace::regnamespace as schema,
  relname as tabela,
  attname as coluna,
  format_type(atttypid,atttypmod) as tipo
from pg_class c
  inner join pg_attribute a
    on (c.oid=a.attrelid)
where attnum>0
  and relnamespace='public'::regnamespace --usar se necessário filtrar um schema
  and relname in ('clientes','tributacao'); --usar se necessário filtrar por tabela

link link link

    
08.02.2017 / 13:46