PostgreSQL, a powerful open-source object-relational database system, offers a rich set of tools and features for managing databases efficiently. Whether you’re a database administrator or a backend developer, knowing how to list and switch between databases is foundational for navigating multi-database environments.
Before you begin, ensure that:
PostgreSQL is installed on your system (psql is accessible).
You have appropriate permissions (e.g., superuser or role with connection rights).
You can authenticate with the PostgreSQL server using a valid user.
Launch the psql interactive terminal and run:
Or the expanded form:
This will return a list of all databases:
Name
Owner
Encoding
Collation
Ctype
Access privileges
Alternatively, run this SQL statement:
This query excludes template databases (template0, template1) and shows user-created databases.
Outside of psql, from your terminal:
You can also use psql -l:
Unlike some other RDBMSs (e.g., MySQL), PostgreSQL does not support switching databases within the same session using a command like USE dbname;.
PostgreSQL establishes a connection to a specific database at login time. To access another database, you must disconnect and reconnect.
Exit the current session and reconnect to the desired database:
Then:
Or directly:
Create a .pgpass file to automate authentication:
Make sure it has correct permissions:
Create a script to list and switch:
Export default DB and user for faster switching:
In PgAdmin:
Click on the server group.
Right-click → Connect to a database.
Use the Query Tool dropdown to switch databases (creates a new tab per DB).
Many tools (DBeaver, DataGrip) allow multiple connections with tabbed database views.
Within psql, find your current database:
Or use:
While PostgreSQL doesn’t allow in-session database switching like some other SQL engines, its robust connection model ensures clean, consistent access control and resource management. By mastering listing techniques and adopting smart reconnection practices, you can efficiently manage and switch between PostgreSQL databases in any environment—whether via CLI, GUI, or scripts.