PostgreSQL Command Line Cheatsheet
PostgreSQL Command Line Cheatsheet
PSQL
Magic words:
psql -U postgres
Some interesting flags (to see all, use -h or --help depending on your psql version):
-E : will describe the underlaying queries of the \ commands (cool for learning!)
-l : psql will list all databases and then exit (useful if the user you connect with
doesn't has a default database, like at AWS RDS)
\q : Quit/Exit
\d+ __table__ : More detailed table definition including description and physical
disk size
\l : List databases
\dt *.* : List tables from all schemas (if *.* is omitted will only show
SEARCH_PATH ones)
\dT+ : List all data types
User Related:
grant __test2__ to __test1__; : Allow __test1__ to set its role as __test2__ .
Configuration
Service management commands:
1) First edit the config file, set a decent verbosity, save and restart postgres:
# Uncomment/Change inside:
log_min_messages = debug5
log_min_error_statement = debug5
log_min_duration_statement = -1
2. Now you will get tons of details of every statement, error, and even background tasks
like VACUUMs
tail -f /var/log/postgresql/postgresql-9.3-main.log
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 2/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
Create command
There are many CREATE choices, like CREATE DATABASE __database_name__ , CREATE
TABLE __table_name__ ... Parameters differ but can be checked at the official
documentation.
Handy queries
SELECT
t.relname AS table_name,
i.relname AS index_name,
a.attname AS column_name
FROM
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a,
pg_namespace n
WHERE
t.oid = ix.indrelid
ORDER BY
t.relname,
i.relname
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 3/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
Execution data:
Queries being executed at a certain DB:
WHERE datname='__database_name__';
Get all queries from all dbs waiting for data (might be hung):
SELECT
pg_stat_get_backend_pid(s.backendid) AS procpid,
pg_stat_get_backend_activity(s.backendid) AS current_query
Casting:
Query analysis:
EXPLAIN __query__ : see the query plan for the given query
EXPLAIN ANALYZE __query__ : see and execute the query plan for the given query
pg_size_pretty(total_database_size) as total_database_size,
schema_name,
table_name,
pg_size_pretty(total_table_size) as total_table_size,
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 4/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
pg_size_pretty(table_size) as table_size,
pg_size_pretty(index_size) as index_size
table_schema as schema_name,
pg_database_size(current_database()) as total_database_size,
pg_total_relation_size(table_name) as total_table_size,
pg_relation_size(table_name) as table_size,
pg_indexes_size(table_name) as index_size
from information_schema.tables
order by total_table_size
) as sizes;
TO { 'filename' | STDOUT }
FROM information_schema.table_privileges
SELECT
r.rolname,
r.rolsuper,
r.rolinherit,
r.rolcreaterole,
r.rolcreatedb,
r.rolcanlogin,
r.rolconnlimit,
r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
r.rolreplication
FROM pg_catalog.pg_roles r
ORDER BY 1;
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 5/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
FROM information_schema.role_table_grants
WHERE table_name='name-of-the-table';
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
Keyboard shortcuts
CTRL + R : reverse-i-search
Tools
ptop and pg_top : top for PG. Available on the APT repository
from apt.postgresql.org .
pg_activity: Command line tool for PostgreSQL server activity monitoring.
Unix-like reverse search in psql:
$ source $HOME/.editrc
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 6/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
This might help to find the size of all tables, their indexes and total database size.
pg_size_pretty(total_database_size) as total_database_size,
schema_name,
table_name,
pg_size_pretty(total_table_size) as total_table_size,
pg_size_pretty(table_size) as table_size,
pg_size_pretty(index_size) as index_size
table_schema as schema_name,
pg_database_size(current_database()) as total_database_size,
pg_total_relation_size(table_name) as total_table_size,
pg_relation_size(table_name) as table_size,
pg_indexes_size(table_name) as index_size
from information_schema.tables
order by total_table_size
) as sizes;
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 7/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
@eodenheimer
\? For Help
https://www.postgresql.org/docs/9.2/sql-copy.html
I was able to prepare ~60,000,000 records in 60 xlsx files, and then I copied them into PG, across 5
tables. It took ~1.5 hours, mostly unattended, and was fairly straightforward. Just decide whether you are
going to use a header row (I strongly recommend doing so) and if you want the copy to include the
primary key. You can find numerous examples online.
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 9/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
\COPY ...
How do we change the default user when using psql in the CLI?
I had installed Postgres 12 before and then uninstalled it entirely (and deleted the entire Postgres 12
folder) and just installed version 13. I used "postgres" as the username, but it keeps showing "dgree" as
my default username. I just don't want to have to type psql -U postgres every single time since there
is not even a user named "dgree" when I use the \du command.
FROM information_schema.table_privileges
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 10/12
5/6/22, 2:05 PM PostgreSQL command line cheatsheet
r.rolname,
r.rolsuper,
r.rolinherit,
r.rolcreaterole,
r.rolcreatedb,
r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
, r.rolreplication
FROM pg_catalog.pg_roles r
ORDER BY 1;
FROM information_schema.role_table_grants
WHERE table_name='name-of-the-table';
--Connections by Database
FROM pg_stat_activity
FROM pg_stat_activity
--Collect statistics of a database (useful to improve speed after a Database Upgrade as previous query
plans are deleted)
ANALYZE VERBOSE;
Informative Thanks for your great effort for making the blog.
Very useful. Are there any resources for Postgres best practices being used in production?
@ganesanb4j I'll add the resource I most like to keep up to date with PG, the Postgres Weekly
newsletter. That's really the only recommendation I can provide you with, but I really like it
https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546 12/12