Skip to content

Instantly share code, notes, and snippets.

View sidymar's full-sized avatar

Sidymar sidymar

View GitHub Profile
@sidymar
sidymar / LISTAR TAMANHO DB.sql
Created May 8, 2017 13:41
Lista o tamanho fisico do BD (postgres)
SELECT pg_size_pretty(pg_database_size('previsao'));
@sidymar
sidymar / LISTAR USUARIOS.sql
Created May 8, 2017 13:40
Lista todos os usuários em um banco de dados postgres.
SELECT u.usename AS "User name",
u.usesysid AS "User ID",
CASE WHEN u.usesuper AND u.usecreatedb THEN CAST('superuser, create
database' AS pg_catalog.text)
WHEN u.usesuper THEN CAST('superuser' AS pg_catalog.text)
WHEN u.usecreatedb THEN CAST('create database' AS
pg_catalog.text)
ELSE CAST('' AS pg_catalog.text)
END AS "Attributes"
FROM pg_catalog.pg_user u
@sidymar
sidymar / LISTAR TRIGGERS.sql
Created May 8, 2017 13:40
Lista triggers em um banco de dados postgresql
SELECT DISTINCT 'DROP TRIGGER ' || trigger_name ||' ON ' || event_object_schema || '.' || event_object_table || ';' FROM information_schema.triggers where trigger_name ilike '%bucardo%' and trigger_schema not ilike '%bucardo%' order by 1
@sidymar
sidymar / DESC TABLES.sql
Created May 8, 2017 13:40
Comando desc tables in postgres.
select
column_name,
data_type,
character_maximum_length
from INFORMATION_SCHEMA.COLUMNS
where table_name = 'condicoes_tempo_metar_ldm'
AND table_schema = 'dados_ocorridos';
@sidymar
sidymar / LINHAS EM COLUNAS.sql
Created May 8, 2017 13:39
Transforma linhas em colunas
SELECT * FROM crosstab
('SELECT gid, inicio_alerta, fim_alerta
from monitoramento.alerta_gis_audit where gid=80287 and (operacao = ''I''or operacao = ''D'')')
AS ct (gid int, inicio timestamp, fim timestamp);
@sidymar
sidymar / EXPORTA USUARIOS.sql
Created May 8, 2017 13:39
Backup de usuarios no Postgres
pg_dumpall -g > roles.sql
@sidymar
sidymar / VALIDA PG_DATA.sh
Created May 8, 2017 13:38
Valida integridade do PG_DATA
#!/bin/sh
# This script verifies that the postgresql data directory has been correctly
# initialized. We do not want to automatically initdb it, because that has
# a risk of catastrophic failure (ie, overwriting a valuable database) in
# corner cases, such as a remotely mounted database on a volume that's a
# bit slow to mount. But we can at least emit a message advising newbies
# what to do.
#
# It's used directly from the systemd service file as a prelaunch check.
with
all_tables as
(
SELECT *
FROM (
SELECT 'all'::text as table_name,
sum( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk,
sum( (coalesce(heap_blks_hit,0) + coalesce(idx_blks_hit,0) + coalesce(toast_blks_hit,0) + coalesce(tidx_blks_hit,0)) ) as from_cache
FROM pg_statio_all_tables --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
) a
select 'drop table \"' || tablename || '\" cascade;' from pg_tables where schemaname='public'
SELECT 'DROP FUNCTION ' || ns.nspname || '.' || proname
|| '(' || oidvectortypes(proargtypes) || ');'
FROM pg_proc INNER JOIN pg_namespace ns ON (pg_proc.pronamespace = ns.oid)
WHERE ns.nspname = 'bucardo' order by proname;