Connect to a specific database

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Opens an interactive psql session to a named database so you can run queries, inspect schema, or manage objects directly.

postgresqlconnectsession

Parameters

Name of the database to connect to.

Commands

Open an interactive session to the specified database

psql -d <database>

Verification

psql -d <database> -c "SELECT current_database();"

The name of the connected database is printed.

Undo

Not undoable

Opening a connection does not change state. Close the session with \q.

Pitfalls

  • The database must exist and the current user must have CONNECT privilege.
  • psql defaults to a database matching the username; use -d to override.

Related