SELECT version();
psql -p 5432 -U postgres -c "CREATE USER NOMEUSUARIO SUPERUSER INHERIT CREATEDB CREATEROLE" -d template1
psql -p 5432 -U postgres -c "ALTER USER NOMEUSUARIO WITH PASSWORD 'SENHA'" -d template1
psql -p 5432 -U postgres -c "SELECT usename FROM pg_user"
OU
psql -p 5432 -U postgres -c "SELECT * FROM pg_user"
psql -p 5432 -U postgres -l
createdb -U postgres NOMEBANCO NOMEBANCO
LATIN1, Collate/CType ISO8859-1:createdb -U postgres -E LATIN1 --locale=pt_BR.iso88591 -T template0 NOMEBANCO
Windows-1252 (WIN1252) é uma superset de ISO-8859-1 (LATIN1)
Todos os caracteres de ISO-8859-1 estão presentes em WIN1252
WIN1252 inclui alguns caracteres extras nos códigos 0x80–0x9F (como € e ‘ ’ “ ”)
createdb -U postgres -E WIN1252 --locale=Portuguese_Brazil.1252 -T template0 NOMEBANCO
psql -p 5432 -U postgres -c "ALTER DATABASE "NOMEBANCO" RENAME TO "NOVONOMEBANCO""
pg_dump --host 127.0.0.1 --port 5432 --username postgres --no-password --format custom --blobs --verbose --file ~/NOMEBANCO.backup NOMEBANCO
pg_restore --host 127.0.0.1 --port 5432 --username postgres --dbname NOMEBANCO --no-password --verbose ~/NOMEBANCO.backup
dropdb -U postgres -p 5432 -h localhost -i -e NOMEBANCO -W
psql -p 5432 -d NOMEBANCO -U postgres -f arquivo.sql
pg_dump --verbose --host 127.0.0.1 --port 5432 --username postgres -d NOMEBANCO > /opt/BDBKP/NOMEBANCO.dmp
psql --host 127.0.0.1 --port 5432 --username postgres --file /opt/BDBKP/NOMEBANCO.dmp NOMEBANCO
psql --host 127.0.0.1 --port 5432 --username postgres -d NOMEBANCO -f /opt/custom/function.sql
psql -p 5432 -d NOMEBANCO -U postgres -c "COMANDO"
psql -d NOMEBANCO -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'NOMEBANCO' AND pid <> pg_backend_pid();"
psql -p 5432 -d NOMEBANCO -U postgres -c "select pg_database_size('NOMEBANCO');"
Equivalente ao du -sh * na pasta base:
SELECT pg_size_pretty(pg_database_size('Database Name'));
SELECT pg_size_pretty(pg_relation_size('table_name'));
SELECT table_name AS "NomeTabela",
pg_size_pretty(pg_total_relation_size('"' || table_name || '"')) AS "Tamanho"
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY pg_total_relation_size('"' || table_name || '"') DESC;
Colunas: Nome, Tamanho (Eq. du -sh) e Tamanho em Bytes (Eq. du -s)
-- Tamanho do Banco
select datname AS "Nome",
pg_size_pretty(pg_database_size(datname)) AS "Tamanho",
pg_database_size(datname) AS tamanho_bytes
FROM pg_database
--where datname = 'Banco';
UNION all
-- Tamanho das tabelas
SELECT table_name AS "Nome",
pg_size_pretty(pg_total_relation_size('"' || table_name || '"')) AS "Tamanho",
pg_total_relation_size('"' || table_name || '"') AS tamanho_bytes
FROM information_schema.tables
WHERE table_schema = 'public'
--ORDER BY pg_total_relation_size('"' || table_name || '"') DESC;
-- 3ª Coluna, para unir o Banco e Tabelas, por tamanho em bytes
ORDER BY tamanho_bytes DESC;
SELECT
table_name,
pg_size_pretty(total_size) AS total_size
FROM (
SELECT
table_name,
pg_total_relation_size(quote_ident(table_schema) || '.' || quote_ident(table_name)) AS total_size
FROM
information_schema.tables
WHERE
table_schema = 'public' -- Substitua pelo esquema desejado, se aplicável
) AS table_sizes
WHERE
total_size >= 10737418240 -- Tabelas de 10 GB ou mais (10 GB = 10 * 1024 * 1024 * 1024 bytes (10×2^30))
ORDER BY
total_size DESC
;
psql -p 5432 -d NOMEBANCO -U postgres -c "\copy (SELECT * FROM TABELA) to '/opt/arquivo.CSV' with csv"
"\copy (SELECT * FROM TABELA) to '/opt/arquivo.CSV' with csv HEADER"
"\copy (SELECT * FROM TABELA) to '/opt/arquivo.CSV' with csv DELIMITER ',' HEADER"
"\copy (SELECT * FROM TABELA) to '/opt/arquivo.CSV' with csv DELIMITER ';' HEADER"
"copy (SELECT * FROM TABELA) to '/opt/arquivo.CSV' (DELIMITER ';');"
"COPY TABELA TO '/opt/arquivo.CSV' DELIMITER ';' NULL 'NULL' CSV HEADER;"
"COPY TABELA TO '/opt/arquivo.CSV' DELIMITER '&' NULL '' CSV HEADER;"
vacuumdb -h 127.0.0.1 -p 5432 -U postgres -w -d NOMEBANCO -v -f -z
vacuumdb -h 127.0.0.1 -p 5432 -U postgres -w -d NOMEBANCO -v -f -t public.TABELA
vacuumdb -h 127.0.0.1 -p 5432 -U postgres -w -d NOMEBANCO -v -Z -t public.TABELA
vacuumdb -h 127.0.0.1 -p 5432 -U postgres -w -d NOMEBANCO -v -f -Z -t public.TABELA
reindexdb -h 127.0.0.1 -p 5432 -U postgres -w -d NOMEBANCO -v
reindexdb -h 127.0.0.1 -p 5432 -U postgres -w -d NOMEBANCO -v -t public.TABELA