PostgreSQL: Administration Commands Reference
Table of Contents
Section titled “Table of Contents”Useful postgresql queries
Section titled “Useful postgresql queries”SELECT pg_reload_conf();Copy table as csv to client filesystem
Section titled “Copy table as csv to client filesystem”\copy table to 'mycsv.csv' DELIMITER ',' CSV HEADER;\copy (SELECT column1, column2 FROM your_table_name WHERE condition) TO 'query_results.csv' DELIMITER ',' CSV HEADER;Get table size
Section titled “Get table size”SELECT pg_size_pretty(pg_total_relation_size('your_table_name'));Get column size
Section titled “Get column size”SELECT sum(pg_column_size(column)) FROM yourtableTransactions
Section titled “Transactions”begin;savepoint my_savepoint;
rollback to savepoint my_savepoint;commit;rollback;Add column
Section titled “Add column”alter table table_name add column column_name data_type [constraint];Delete
Section titled “Delete”begin;DELETE FROM table_name where condition;-- commit;Update
Section titled “Update”UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;Deadlocks
Section titled “Deadlocks”SELECT deadlocks FROM pg_stat_database WHERE datname = current_database();pg_stat_activity: Shows current connections and their states, including those waiting for locks. pg_locks: Provides details about locks held and requested, including the object being locked (table, row, etc.) and the process ID (PID) involved.
Table sizes
Section titled “Table sizes”SELECT relname AS table_name, pg_size_pretty(pg_total_relation_size(oid)) AS total_sizeFROM pg_classWHERE relkind = 'r' -- 'r' for regular tableORDER BY pg_total_relation_size(oid) DESCLIMIT 10;Upgrades
Section titled “Upgrades”mv /opt/pgdata/data_5432 /opt/pgdata/data_5432.pg16ls -l /opt/pgdata/data_5432.pg16/opt/pg-17/bin/initdb --no-locale --auth="ident" -D /opt/pgdata/data_5432 -X /opt/pgdata/dblog/pg_wal_5432convert postgres config postgresql.confcopy pg_hba.conf overpg_upgrade --old-datadir --new-datadir --old-bindir -new-bindir --jobs=6 --check --verbosepg_upgrade --old-datadir --new-datadir --old-bindir -new-bindir --jobs=6 --link --verboseldconfigVacuum
Section titled “Vacuum”-- Aggressively clean dead tuples and reclaim space for current DBVACUUM (FULL, ANALYZE, VERBOSE);SELECT name, setting, unit FROM pg_settings WHERE name IN ( 'shared_buffers', 'temp_buffers', 'work_mem', 'maintenance_work_mem', 'wal_buffers', 'effective_cache_size' );
Gotcha — so 32 GB RAM box running both PostgreSQL and the web app.Your current shared_buffers = 98304 (768 MB) is very conservative for that machine.Typical starting point for mixed-use servers:Total RAM: 32 GBLeave for OS + file cache + web stack: ~16–20 GBGive PostgreSQL: ~12–16 GB total headroomSet shared_buffers to about 3–6 GBExamples (PostgreSQL uses 8k pages):3 GB → shared_buffers = 393216 (3 * 1024^3 / 8192)4 GB → shared_buffers = 5242886 GB → shared_buffers = 786432I’d start with 4 GB and monitor:shared_buffers = 4GBeffective_cache_size = 16GBwork_mem = 32MBmaintenance_work_mem = 1GB
ALTER SYSTEM SET shared_buffers = '4GB';ALTER SYSTEM SET temp_buffers = '64MB';ALTER SYSTEM SET work_mem = '32MB';ALTER SYSTEM SET maintenance_work_mem = '1GB';ALTER SYSTEM SET wal_buffers = '16MB';ALTER SYSTEM SET effective_cache_size = '16GB';
SELECT pg_reload_conf();