Skip to content

Instantly share code, notes, and snippets.

@wendimust
Last active March 31, 2025 15:23
Show Gist options
  • Select an option

  • Save wendimust/d228def3b96228f03ba3030814745930 to your computer and use it in GitHub Desktop.

Select an option

Save wendimust/d228def3b96228f03ba3030814745930 to your computer and use it in GitHub Desktop.
Moving around in PostgreSQL with the CLI

Here’s a quick guide to basic navigation inside a PostgreSQL database using the command-line interface (CLI).

  1. Access PostgreSQL To start using PostgreSQL, open a terminal and enter:
psql -U your_username -d your_database

Replace your_username with your PostgreSQL username and your_database with the name of the database you want to access.

If you're connecting as the default PostgreSQL user (postgres), use:

psql -U postgres

If PostgreSQL requires a password, enter it when prompted.

  1. List Databases Once inside the PostgreSQL shell, list all databases with:
\l

or:

\list
  1. Switch to a Different Database To connect to another database:
\c database_name

or:

\connect database_name
  1. List Tables in the Current Database To see all tables in the database:
\dt

For more details about tables:

\dt+
  1. Describe a Table Structure To see the structure of a specific table:
\d table_name

For extended details:

\d+ table_name
  1. List Schemas To view all schemas in the database:
\dn
  1. List Users (Roles) To see all users (roles) in the PostgreSQL instance:
\du
  1. Run a Query Execute SQL commands, such as selecting all rows from a table:
SELECT * FROM table_name;
  1. Exit the PostgreSQL Shell To leave the psql prompt, type:
\q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment