Notes from a postgres admin 101 course. Contains a mix of "good-to-know" information about the workings of PostgreSQL and practical tools. ⚠️ This guide is intended for database administration, not usage, and does not cover SQL or "user" information.
netstat -nap | grep postgres # To grab listening port if not default
ps uf -C postgres # List all running instances w/ process owner
ps f -U *instance_owner* # With user found above, to see startup args
psql -c SHOW data_directory; # To get the main cluster directory aka data directory
psql -c SHOW config_file; # To get the config file path, useful if nondefault
su - *db_owner* pg_controldata *data_directory* # To get cluster details
/var/lib/pgsql/<version>/data by defaultpostgres.conf in /etc by default
|
name
|
Setting name
|
|
value
|
Current value
|
|
context
|
Context of the setting (what to restart to update the value)
|
|
source/sourcefile/sourceline
|
Where the setting is defined
|
|
setting/unit
|
Value of the setting
|
|
boot_val
|
Value at instance startup
|
|
reset_val
|
Default value
|
|
pending_restart
|
Value has pending modifications
|
pg_default tablespace by default: hardcoded to base subdirectory of the cluster directory. All objects (including entire DBs) can be moved to tablespaces located in any accessible path. Once declared, a "tablespace folder" is an integral, non-optional part of the cluster.|
initdb
|
Create file/dir structure (aka cluster) for instance
|
|
createdb, dropdb
|
Create/delete a DB
|
|
pg_ctl
|
Control instance state
|
|
pg_controldata
|
View config
|
|
pg_isready
|
Check if instance is up w/o opening a full connection
|
|
pg_resetwal
|
DANGEROUS Wipe transaction logs
|
public group.|
Can connect according to pg_hba
|
|
Has LOGIN permission
|
|
Has CONNECT on DB
|
|
Has USAGE on schema/namespace
|
|
Has <operation> on table/column of queryable
|
initdb \
-A auth_method \
-D data_directory \
-E encoding \
-X tlog_directory \
--locale=locale \
[-k]
# Start from service
systemctl enable postgres
systemctl start postgres
# Start standalone
/usr/pgsql-16/bin/pg_ctl -D data_directory start
# Connect as postgres w/o passwd
sudo -u postgres psql -w
|
TYPE
|
local | host
|
|
DATABASE
|
all | replication | db_name
|
|
USER
|
role for specific role, +role for group |
|
ADDRESS
|
Origin address
|
|
METHOD
|
Accepted auth method, see man page
|
|
pg_dump
|
Logical dumps of single DB
|
|
pg_dumpall
|
Logical dumps of entire instance
|
|
pg_restore
|
Apply logical dumps for restoration
|
|
archiver
|
Archive transaction logs, see archive_command in postgresql.conf
|
|
pg_backup_start()
|
Prepares DB for physical backup
|
|
pg_backup_stop()
|
Ends physical backup process and returns missing data necessary for backed up cluster to be consistent
|
|
pg_basebackup
|
Physical backup automation tool, does pg_backup_* calls and file copies automatically
Only tool available on Windows |
|
pg_verifybackup
|
Check "plain" type backup integrity
|
|
pg_receivewal
|
Transparent, pull-style backup and WAL archiver tool
|
|
pgBackRest
|
Backup/restore utility
|
|
pgBarman
|
Backup/restore utility (prefer pgBackRest)
|
|
Connect from system shell
|
psql -h hostname -p port -U role -d DB
|
|
Connect from psql shell
|
\c db user host port
|
|
Execute sql script
|
\i file
|
|
Execute shell command
|
\! command
|
|
Edit psql options
|
\set [variable=value]
|
|
Execute command every 3 seconds 2 times
|
\watch i=2 c=3
|
|
Get help on SQL command
|
\h command
|
|
Edit command in external editor and execute
|
\e
|
|
Make wide tables readable
|
\x on|auto
|
|
Profile script
|
Located in ~/.psqlrc
|
|
Re-run last command
|
\g (\gx to output as \x on)
|
|
\l
|
Databases
|
|
\dt
|
Tables
|
|
\dn
|
Namespaces (aka schemas)
|
|
\d name
|
Describe queryable or index
|
|
\d
|
List everything
|
|
\di
|
Indexes
|
|
\ds
|
Sequences
|
|
\dp
|
Permissions
|
|
\du
|
Roles (users/groups)
|
|
\dv
|
Views
|
|
\dx
|
Extensions
|
|
\dn
|
Namespaces (schemas)
|
|
Alter for this session
|
SET search_path TO my_db;
|
|
Persist for this DB
|
ALTER DATABASE SET search_path to ... ;
ALTER DATABASE SET search_path FROM CURRENT; |
|
Persist for this instance
|
postgresql.conf
|
\d) only show what is in the search path by default.
|
Backend processes transactions by loading cache pages in memory from datafiles and updating them. One per user session
|
|
WAL writer watches WAL buffers and flushes them to disk periodically
|
|
BG writer watches for individual dirty pages in shared memory and writes them to datafiles.
|
|
Checkpointer periodically uses WAL to flush all shared memory written before a checkpoint (automatic or user-requested) to disk.
|
|
Autovacuum periodically reclaims invalidated cache pages.
|
|
pg_activity
|
top-like monitor |
|
dbeaver
|
GUI tool for DDL visualization
|
|
pgadmin
|
Web-based tool (slow)
|
|
pgloader
|
Data Migration tool from other psql instance, CSV or other DBMS
|
|
pgHero
|
Performance dashboard for Postgres
|
|
pgTune
|
Performance configuration tuning tool
|
|
pgBadger
|
Web-based monitoring tool (eq. OEM reports)
|
|
no extension
|
Tables, indexes
|
|
.TOAST
|
Oversized object storage
|
|
.FSM
|
Free space map
|
|
.VM
|
Visibility map
|
|
pg_wal
|
Write-ahead log
|
|
pg_xact
|
Commit data
|
|
pg_commit_ts
|
Commit timestamps
|
|
pg_multixacts
|
States of multiple transactions
|
|
pg_serial
|
States of serializable transactions
|
|
pg_twophase
|
States of prepared transactions
|
|
pg_dynshmem
|
Dynamic shared memory
|
|
pg_logical
|
Logical replication
|
|
pg_notify
|
Listen/Notify states
|
|
pg_repslot
|
Replication slots
|
|
pg_snapshots
|
Exported snapshots
|
pg_stat_archiver gives info about archiving processes.noatime, data=writeback|
pg_freespacemap
|
View free space maps
|
|
pg_prewarm
|
Preload caches from last run at boot
|
|
pg_stat_statements
|
Track SQL execution statistics
|
|
auto_explain
|
Trace costly statements automatically
|
|
pgstattuple
|
Get table stats (live/dead rows, volume of data)
|
\dx[+ extension]|
pg_locks
|
Locks
|
|
pg_stat_database
|
DB-wide object statistics
|
|
pg_class
|
Object-ID mappings
|
|
pg_stat_*_tables
|
Table-level statistics
|
|
pg_stats
|
Column-level stats
|
|
pg_archiver_stats
|
Archiver status
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.