01 Quais tabelas existem?
1SELECT table_name
2FROM information_schema.tables
3WHERE table_schema = 'public'
4 AND table_type = 'BASE TABLE'
5ORDER BY table_name;
information_schema é o catálogo interno do PostgreSQL — ele mostra toda a estrutura do banco.
02 Quais colunas existem na tabela?
1SELECT
2 ordinal_position AS ordem,
3 column_name AS coluna,
4 data_type AS tipo,
5 character_maximum_length AS tamanho,
6 is_nullable AS aceita_nulo,
7 column_default AS valor_padrao
8FROM information_schema.columns
9WHERE table_name = 'servidor'
10ORDER BY ordinal_position;
USER-DEFINED geralmente significa ENUM (tipo personalizado).
03 Como são os dados?
1-- ver dados
2SELECT * FROM servidor LIMIT 5;
3
4-- contagem total
5SELECT COUNT(*) FROM servidor;
6
7-- valores únicos
8SELECT DISTINCT situacao FROM servidor;
Sempre use LIMIT no início. Em produção, tabelas podem ter milhões de registros.
04 Como as tabelas se conectam?
1SELECT
2 tc.table_name,
3 kcu.column_name,
4 ccu.table_name AS ref_table,
5 ccu.column_name AS ref_column
6FROM information_schema.table_constraints tc
7JOIN information_schema.key_column_usage kcu
8 ON tc.constraint_name = kcu.constraint_name
9JOIN information_schema.constraint_column_usage ccu
10 ON ccu.constraint_name = tc.constraint_name
11WHERE tc.constraint_type = 'FOREIGN KEY';
Isso mostra exatamente como fazer JOIN entre tabelas.
05 Estatísticas rápidas
1SELECT
2 COUNT(*) AS total,
3 COUNT(salario_atual) AS preenchidos,
4 COUNT(*) - COUNT(salario_atual) AS nulos,
5 MIN(salario_atual),
6 MAX(salario_atual),
7 ROUND(AVG(salario_atual), 2)
8FROM servidor;
1SELECT situacao, COUNT(*)
2FROM servidor
3GROUP BY situacao
4ORDER BY COUNT(*) DESC;
06 ENUMs, views e funções
1-- enums do banco
2SELECT t.typname, e.enumlabel
3FROM pg_type t
4JOIN pg_enum e ON e.enumtypid = t.oid;
1-- listar views
2SELECT table_name
3FROM information_schema.views;
1-- código da view
2SELECT view_definition
3FROM information_schema.views
4WHERE table_name = 'vw_servidores';
Views já possuem JOINs prontos — use-as como ponto de partida.
Fluxo do analista
01 → Descobrir tabelas  ·  02 → Entender colunas  ·  03 → Ver dados  ·  04 → Entender relações
Depois disso você já consegue fazer análises reais.