2 Ways to Show All Databases in PostgreSQL (psql)

Here are a couple of ways to show a list of databases when using psql with PostgreSQL.

The first option can be used when we’re already connected to PostgreSQL. The second option can be used when we don’t currently have a connection to Postgres.

The \l and \list Commands

We can use either \l or \list to return a list of databases.

The syntax goes like this:

\l[+] or \list[+] [ pattern ]

The parts in square brackets [] are optional.

So the quickest/easiest way to get a list of databases is like this:

\l

Example result:

                                    List of databases
+--------------+----------+----------+-------------+-------------+-----------------------+
|     Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |
+--------------+----------+----------+-------------+-------------+-----------------------+
| barney       | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       |
| krankykranes | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       |
| music        | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       |
| pagila       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       |
| pethotel     | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       |
| postgres     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       |
| template0    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|
|              |          |          |             |             | postgres=CTc/postgres |
| template1    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|
|              |          |          |             |             | postgres=CTc/postgres |
+--------------+----------+----------+-------------+-------------+-----------------------+

Just to be clear, I ran that command when I was already connected to PostgreSQL.

The same result can be had by using \list instead of \l.

We can add a plus sign (+) to return more information about each table:

\l+

Example result:

                                                                      List of databases
+--------------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------+
|     Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description                 |
+--------------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------+
| barney       | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8473 kB | pg_default |                                            |
| krankykranes | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8289 kB | pg_default |                                            |
| music        | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8225 kB | pg_default |                                            |
| pagila       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 16 MB   | pg_default |                                            |
| pethotel     | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8177 kB | pg_default |                                            |
| postgres     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8097 kB | pg_default | default administrative connection database |
| template0    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7905 kB | pg_default | unmodifiable empty database                |
|              |          |          |             |             | postgres=CTc/postgres |         |            |                                            |
| template1    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7905 kB | pg_default | default template for new databases         |
|              |          |          |             |             | postgres=CTc/postgres |         |            |                                            |
+--------------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------+

So we get a few extra columns with info about size, tablespace, etc.

We can also use a pattern to return only those databases that match the pattern:

\l krank*

Example result:

                                  List of databases
+--------------+----------+----------+-------------+-------------+-------------------+
|     Name     |  Owner   | Encoding |   Collate   |    Ctype    | Access privileges |
+--------------+----------+----------+-------------+-------------+-------------------+
| krankykranes | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                   |
+--------------+----------+----------+-------------+-------------+-------------------+

The -l and --list Connection Options

When we’re not currently connected to Postgres, we can connect using the -l or --list connection option.

When one of these options are specified, psql will connect to Postgres, list all available databases, then exit.

It can be used in the following ways:

-l
--list

So instead of using the backslash, it’s either a hyphen or two hyphens (depending on which one you use).

To use this option, open a new terminal window or command prompt and type the following:

psql -l

Assuming it’s in your PATH variable, that should launch PostgreSQL, list all databases, then exit.

Example result:

                                   List of databases
     Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
--------------+----------+----------+-------------+-------------+-----------------------
 barney       | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 krankykranes | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 music        | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 pagila       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 pethotel     | barney   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
              |          |          |             |             | postgres=CTc/postgres
 template1    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
              |          |          |             |             | postgres=CTc/postgres

The same can be applied using --list:

psql --list