PSQL Shell¶
Renaming¶
Renaming a table¶
Renaming a column¶
Investigating¶
List all tables¶
\dtfor the current schema\dt schema_name.*\dt *.table_name- to find all tables with that name
List all column names¶
\d+ <table_name>
SELECT *
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table';
Debugging PSQL¶
Long running queries¶
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
Blocked PIDs¶
select pid,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
Recover your database after your computer suddenly turns off¶
- Make sure there's no
postgresprocessesps aux | ag postgres
rm -f /usr/local/var/postgres/postmaster.pidbrew services restart postgresql/usr/local/opt/postgres/bin/createuser -s postgres- create the
postgresuser
- create the
Create a CSV¶
- -i: launch shell with super user privileges
- -u: user
Last update:
2023-04-24