PostgreSQL Cheat Sheet/Connect to a database

Open an interactive psql session to a database.

Section: Connect and Inspect with psql

Connect to a database

bash
bash
psql -d appdb
Explanation

Use environment variables like PGHOST, PGPORT, PGUSER, and PGPASSWORD to avoid repeating connection flags.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Connect and Inspect with psql
Connect with URI
Connect using a full PostgreSQL connection string.
OpenIn sheetbashsame section
List databases
Show all databases in the cluster.
OpenIn sheetsqlsame section
List roles
Show roles and memberships.
OpenIn sheetsqlsame section
List schemas
Show schemas in the current database.
OpenIn sheetsqlsame section
List tables views and sequences
List relations across schemas.
OpenIn sheetsqlsame section
Describe a table
Show columns, indexes, defaults, storage details, and more for a relation.
OpenIn sheetsqlsame section