PostgreSQL is one of the most popular open-source database systems. This article will show you how to list all databases in PostgreSQL from the command line with the \l command.
Prerequisites
To use PostgreSQL, you’ll need to have a PostgreSQL database server set up, and have the psql PostgreSQL command line tools installed.
Connecting to the Server
If you are running your database on the same machine that you are connecting from and are connecting as the default postgres user, you can simply log in using:
psql -U postgres
If you are connecting to a remote database or want to log in as a different user, see our instructions here.
Listing all Databases in PostgreSQL
Once connected to a PostgreSQL server using the psql program, simply issue the following command to list all databases:
\l
This will output the names of all databases in the PostgreSQL cluster that you are connected to
If your user has permission to, you can immediately connect to a database without exiting psql by running:
\c DATABASE_NAME
…obviously replacing DATABASE_NAME with the name of the databases from the listed results.
Check Which Database You Are Connected To
To confirm that you have successfully connected to the database, or check which you are connected to, use the following command:
\conninfo
This will output information including the database and user names currently in use, and the address of the database server.