psql and Admin Basics
Day-to-day psql and admin control plane commands. Results appear in the same fence: same-line -- comments when short, multiline -- blocks below the sample when not.
Search across all documentation pages
Day-to-day psql and admin control plane commands. Results appear in the same fence: same-line -- comments when short, multiline -- blocks below the sample when not.
Connect with connection URI or flags.
-- psql "postgres://user@localhost:5432/app"
-- psql -h localhost -U user -d appList and describe tables.
-- \dt
-- \d users
-- columns, indexes, FKsPrivileges on tables and schemas.
GRANT SELECT, INSERT ON ALL TABLES IN SCHEMA app TO app_rw;
REVOKE ALL ON SCHEMA app FROM PUBLIC;Reclaim and refresh planner stats.
VACUUM (ANALYZE) orders;
-- updates stats for ordersSession/local GUCs inside a transaction.
BEGIN;
SET LOCAL statement_timeout = '5s';
SELECT pg_sleep(1);
COMMIT;
-- timeout applies only in that transactionToggle query timing in psql.
-- \timing on
-- Time: 12.345 msClient-side COPY for local files.
-- \copy users(email) FROM 'users.csv' CSV HEADERLogin roles and groups.
CREATE ROLE app_ro LOGIN PASSWORD '...';
GRANT CONNECT ON DATABASE app TO app_ro;Who am I / which DB.
SELECT current_user, current_database();
-- role | dbSee running sessions.
SELECT pid, usename, state, query
FROM pg_stat_activity
WHERE datname = current_database();
-- active backendsCancel or terminate a backend pid.
SELECT pg_cancel_backend(12345);
-- t if signal sentEnable extensions in a database.
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
-- tracking extension installedTable and index sizes.
SELECT pg_size_pretty(pg_total_relation_size('orders'));
-- e.g. 128 MBWatch wraparound risk (ops).
SELECT datname, age(datfrozenxid)
FROM pg_database
ORDER BY 2 DESC
LIMIT 5;
-- higher age = older xid horizonEdit buffer in $EDITOR from psql.
-- \e
-- opens editor with last queryStack versions: PostgreSQL 18.4 (stable 18, maintenance 17) · pgvector 0.8+
Reviewed by Chris St. John·Last updated Jul 18, 2026