PostgreSQL is a powerful open-source relational database system. If you spend much time inside the psql interactive shell, you’ll want a handy reference to the most commonly used commands. This guide provides a quick lookup for listing databases, users, tables, and more.
All examples below assume you are inside the psql prompt, unless noted otherwise.
Connecting and Exiting
Before you can explore your PostgreSQL environment, you need to connect:
|
1 2 |
psql -U username -d database |
This command connects you to the database using the specified username. Once you’re finished, you can quit out of psql with:
|
1 2 |
\q |
Working with Databases
PostgreSQL can host multiple databases within the same cluster. These commands help you view and switch between them:
|
1 2 3 4 |
\l -- List all databases \c dbname -- Connect to a different database SELECT current_database(); -- Show the current database |
Listing Users and Roles
Roles in PostgreSQL can act as both users and groups. To see who exists in your system:
|
1 2 3 4 |
\du -- List all users and roles \dg -- Same as \du SELECT usename FROM pg_user; -- Query all users |
Exploring Tables and Schemas
Schemas provide logical separation of tables and other objects. To explore what’s available:
|
1 2 3 4 5 |
\dt -- List tables in the current schema \dt *.* -- List tables across all schemas \d tablename -- Show table structure \dn -- List all schemas |
Checking Connection and Environment Info
These commands provide information about your session and environment:
|
1 2 3 4 |
\conninfo -- Show current connection info \encoding -- Display client encoding \password -- Change password for the current user |
Useful Queries
A few SQL queries you’ll often find helpful:
|
1 2 3 4 |
SELECT version(); -- PostgreSQL version SELECT current_user; -- Show current user SELECT now(); -- Current timestamp |
Administrative Queries
To get a quick overview of system-level information:
|
1 2 3 |
SELECT datname FROM pg_database; -- List all databases SELECT rolname FROM pg_roles; -- List all roles |
Conclusion
These commands cover the basics you’ll use most often in PostgreSQL. Whether you’re a developer, database administrator, or just getting started, having a quick reference can save you time and reduce frustration.
If you need help with PostgreSQL hosting, administration, or performance tuning, the Reliable Penguin team can assist. We provide managed hosting and expert support to keep your databases running smoothly.




