A short refresher for navigating PostgreSQL databases, schemas, and objects from the psql command line.
Schemas
List all schemas:
\dn
Enter fullscreen mode Exit fullscreen mode
List schemas with additional information:
\dn+
Enter fullscreen mode Exit fullscreen mode
Check the current schema search path:
SHOW search_path;
Enter fullscreen mode Exit fullscreen mode
Set the schema search path for the current session:
SET search_path TO schema_name;
Enter fullscreen mode Exit fullscreen mode
Databases
List databases:
\l
Enter fullscreen mode Exit fullscreen mode
Connect to another database:
\c database_name
Enter fullscreen mode Exit fullscreen mode
Show the current connection:
\conninfo
Enter fullscreen mode Exit fullscreen mode
Show the current database:
SELECT current_database();
Enter fullscreen mode Exit fullscreen mode
Show the current user:
SELECT current_user;
Enter fullscreen mode Exit fullscreen mode
Tables
List tables in the current schema:
\dt
Enter fullscreen mode Exit fullscreen mode
List tables in a specific schema:
\dt schema_name.*
Enter fullscreen mode Exit fullscreen mode
List tables across all schemas:
\dt *.*
Enter fullscreen mode Exit fullscreen mode
List tables with additional information:
\dt+
Enter fullscreen mode Exit fullscreen mode
Show the structure of a table:
\d table_name
Enter fullscreen mode Exit fullscreen mode
Show detailed information about a table:
\d+ table_name
Enter fullscreen mode Exit fullscreen mode
Other Database Objects
List views:
\dv
Enter fullscreen mode Exit fullscreen mode
List functions:
\df
Enter fullscreen mode Exit fullscreen mode
List indexes:
\di
Enter fullscreen mode Exit fullscreen mode
List sequences:
\ds
Enter fullscreen mode Exit fullscreen mode
List installed extensions:
\dx
Enter fullscreen mode Exit fullscreen mode
Exit psql
\q
Enter fullscreen mode Exit fullscreen mode
These commands cover most of the basic navigation and object discovery you’ll need when working with PostgreSQL from the command line.