Skip to main content

PostgreSQL Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for connecting to PostgreSQL, managing databases and roles, granting privileges, and running backup commands.

PostgreSQL handles everything from local development databases to production application backends. This cheatsheet covers common `psql` connection patterns, database and role management, privilege commands, introspection queries, and backup basics.

Basic Syntax

Core PostgreSQL command forms.

CommandDescription
psqlOpen an interactive PostgreSQL shell using local defaults
psql -U user -d dbnameConnect as a specific user to a specific database
psql -h host -p 5432 -U user -d dbnameConnect to a remote PostgreSQL server
psql -c "SQL_STATEMENT"Run one SQL command and exit
sudo -u postgres psqlOpen psql as the local postgres superuser

Connect and Switch

Common ways to connect and move between databases.

CommandDescription
sudo -u postgres psqlConnect locally as the postgres system user
psql -U app_user -d app_dbConnect to app_db as app_user
psql "host=localhost port=5432 dbname=app_db user=app_user"Connect with a connection string
\c app_dbSwitch to another database inside psql
\conninfoShow the current connection details

Databases

Create, list, rename, and remove databases.

CommandDescription
CREATE DATABASE app_db;Create a new database
CREATE DATABASE app_db OWNER app_user;Create a database owned by a specific role
\lList databases
ALTER DATABASE app_db RENAME TO app_prod;Rename a database
DROP DATABASE app_db;Delete a database

Roles and Users

Create login roles and inspect existing roles.

CommandDescription
CREATE ROLE app_user;Create a role without login
CREATE ROLE app_user WITH LOGIN PASSWORD 'strong_password';Create a login role
CREATE USER app_user WITH PASSWORD 'strong_password';Shortcut for a login role
ALTER ROLE app_user WITH PASSWORD 'new_password';Change a role password
\duList roles and attributes

Grant and Revoke Privileges

Give or remove access at the database, schema, and table levels.

CommandDescription
GRANT CONNECT ON DATABASE app_db TO app_user;Allow a role to connect to a database
GRANT USAGE, CREATE ON SCHEMA public TO app_user;Allow schema access and object creation
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE orders TO app_user;Grant table privileges
REVOKE INSERT, UPDATE ON TABLE orders FROM app_user;Remove selected table privileges
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO app_user;Grant defaults for future tables

Table and Schema Introspection

Inspect schemas, tables, columns, and query results.

CommandDescription
\dnList schemas
\dtList tables in the current search path
\dt public.*List tables in the public schema
\d ordersDescribe a table, view, or sequence
SELECT * FROM orders LIMIT 10;Preview rows from a table

psql Meta-Commands

Useful built-in psql commands for daily administration.

CommandDescription
\?Show psql meta-command help
\h CREATE ROLEShow SQL help for one statement
\xToggle expanded output for wide rows
\timingToggle query timing display
\qQuit psql

Backup and Restore

Common logical backup and restore commands.

CommandDescription
pg_dump -U app_user -d app_db > app_db.sqlExport a database as plain SQL
pg_dump -Fc -U app_user -d app_db -f app_db.dumpCreate a custom-format backup
psql -U app_user -d app_db < app_db.sqlRestore a plain SQL dump
pg_restore -U app_user -d app_db app_db.dumpRestore a custom-format dump
pg_dumpall > cluster.sqlBack up all databases and global objects

Version and Service Checks

Quick checks for server version and service status.

CommandDescription
SELECT version();Show the PostgreSQL server version
psql --versionShow the client version
SHOW server_version;Show the server version only
sudo systemctl status postgresqlCheck the PostgreSQL service state
sudo systemctl restart postgresqlRestart the PostgreSQL service

Use these guides for full PostgreSQL walkthroughs.

GuideDescription
PostgreSQL User Management: Create Users and Grant PrivilegesFull guide to roles, passwords, and grants
How to Check the PostgreSQL VersionFind the installed and running PostgreSQL version
How to Install PostgreSQL on Ubuntu 20.04Install PostgreSQL on Ubuntu
How to Install PostgreSQL on Debian 10Install PostgreSQL on Debian
How to Install PostgreSQL on CentOS 8Install PostgreSQL on CentOS