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?
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?
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>';
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