PostgreSQL Administration Cheat Sheet

Server lifecycle, utility commands, grants, monitoring, and cluster maintenance tasks.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Server Control and Cluster Management

Start, stop, and inspect PostgreSQL server processes.

Initialize cluster

Create a new PostgreSQL data directory.

bashANYpostgresqlinitdbcluster
bash
initdb -D /var/lib/postgresql/data

Run once when creating a new cluster.

Start server with pg_ctl

Start a PostgreSQL server from a data directory.

bashANYpostgresqlpg_ctlstart
bash
pg_ctl -D /var/lib/postgresql/data -l logfile start

See summary for usage details.

Stop server fast

Stop the server with fast shutdown mode.

bashANYpostgresqlpg_ctlstop
bash
pg_ctl -D /var/lib/postgresql/data stop -m fast

See summary for usage details.

Restart server

Restart the server cleanly.

bashANYpostgresqlpg_ctlrestart
bash
pg_ctl -D /var/lib/postgresql/data restart

See summary for usage details.

Check readiness

Probe whether the server is accepting connections.

bashANYpostgresqlhealthcheck
bash
pg_isready -h localhost -p 5432 -d appdb

See summary for usage details.

Show data directory

Inspect the server's active data directory from SQL.

sqlANYpostgresqlserverinspect
sql
SHOW data_directory;

See summary for usage details.

Show config file path

Find the active `postgresql.conf` path.

sqlANYpostgresqlconfiginspect
sql
SHOW config_file;

See summary for usage details.

Reload config

Ask PostgreSQL to reload configuration files.

sqlANYpostgresqlconfigreload
sql
SELECT pg_reload_conf();

See summary for usage details.

Database Administration Utilities

Cluster-level utilities for common admin tasks.

Create database from shell

Create a database using the `createdb` utility.

bashANYcreatedbdatabase
bash
createdb -h localhost -U postgres appdb

See summary for usage details.

Drop database from shell

Drop a database using the `dropdb` utility.

bashANYdropdbdatabasedanger
bash
dropdb -h localhost -U postgres appdb

See summary for usage details.

Create role from shell

Interactively create a new role.

bashANYcreateuserrole
bash
createuser -h localhost -U postgres --interactive

See summary for usage details.

Drop role from shell

Remove a role using the command-line utility.

bashANYdropuserrole
bash
dropuser -h localhost -U postgres olduser

See summary for usage details.

Reindex database

Rebuild indexes in a database.

bashANYreindexdbmaintenance
bash
reindexdb -h localhost -U postgres -d appdb --concurrently

See summary for usage details.

Vacuum and analyze

Run vacuum/analyze from the shell.

bashANYvacuumdbanalyze
bash
vacuumdb -h localhost -U postgres -d appdb --analyze-in-stages

See summary for usage details.

Physically reorder table

Rewrite a table using an index order.

sqlANYclustermaintenancetable
sql
CLUSTER orders USING idx_orders_created_at;

Can improve locality for certain access patterns.

Collect planner stats

Refresh planner statistics for a table.

sqlANYanalyzeplannerstats
sql
ANALYZE public.users;

See summary for usage details.

Monitoring and Statistics

Catalog views and functions used in ops and debugging.

Show active sessions

Inspect current sessions and queries.

sqlANYpg_stat_activitymonitoring
sql
SELECT pid, usename, application_name, state, query
FROM pg_stat_activity
ORDER BY backend_start;

See summary for usage details.

Cancel a query

Cancel a running query without terminating the session.

sqlANYcancelbackendops
sql
SELECT pg_cancel_backend(pid)
FROM pg_stat_activity
WHERE pid = 12345;

See summary for usage details.

Terminate session

Force a backend to disconnect.

sqlANYterminatebackendops
sql
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid = 12345;

See summary for usage details.

Inspect locks

Inspect lock acquisition and contention.

sqlANYlocksmonitoring
sql
SELECT pid, relation::regclass, mode, granted
FROM pg_locks
ORDER BY relation, mode;

See summary for usage details.

Show database size

Measure one database's size.

sqlANYsizedatabase
sql
SELECT pg_size_pretty(pg_database_size('appdb'));

See summary for usage details.

Show table and indexes size

Measure one relation including indexes and TOAST.

sqlANYsizetable
sql
SELECT pg_size_pretty(pg_total_relation_size('public.users'));

See summary for usage details.

Inspect bgwriter stats

Review checkpoint and buffer write statistics.

sqlANYbgwriterstats
sql
SELECT * FROM pg_stat_bgwriter;

See summary for usage details.

Find unused indexes

Review index scan counts when cleaning up indexes.

sqlANYindexesmonitoringstats
sql
SELECT schemaname, relname, indexrelname, idx_scan
FROM pg_stat_user_indexes
ORDER BY idx_scan ASC, relname;

See summary for usage details.

Security, Grants, and Default Privileges

Ownership, grants, and privilege management.

Grant table select

Allow read access to a table.

sqlANYgranttablesecurity
sql
GRANT SELECT ON TABLE public.users TO readonly;

See summary for usage details.

Grant schema usage

Allow object lookup inside a schema.

sqlANYgrantschemasecurity
sql
GRANT USAGE ON SCHEMA public TO readonly;

See summary for usage details.

Grant sequence usage

Allow use of sequences for inserts or reads.

sqlANYgrantsequence
sql
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO appuser;

See summary for usage details.

Revoke update privilege

Remove write capability from a role.

sqlANYrevoketable
sql
REVOKE UPDATE ON TABLE public.users FROM readonly;

See summary for usage details.

Alter default privileges

Set permissions that future objects should inherit.

sqlANYdefault-privilegesgrant
sql
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO appuser;

See summary for usage details.

Change object owner

Transfer ownership of an object to another role.

sqlANYownersecurity
sql
ALTER TABLE public.users OWNER TO appuser;

See summary for usage details.

Set active role

Temporarily assume another role for the session.

sqlANYset-rolesessionsecurity
sql
SET ROLE readonly;

See summary for usage details.

Reset active role

Return to the original session authorization.

sqlANYreset-rolesecurity
sql
RESET ROLE;

See summary for usage details.

Recommended next

No recommendations yet.