PostgreSQL does not support the SHOW DATABASES command. Instead, you can list databases in PostgreSQL using the \l or \l+ meta-commands in the psql terminal or by querying the pg_database system catalog.
- Use \l or \l+ to display databases through the psql terminal.
- Query pg_database to retrieve database names using SQL.
- These methods help you view databases available on the PostgreSQL server.
Method 1: Using the psql Command
To list all databases present on the current PostgreSQL server, use one of the following psql meta-commands.
\lor
\l+Now use the below command to list all databases using a superuser such as postgres:
\lOutput:
Alternatively one can use the below command:
\l+Output:
Method 2: Using the SELECT Statement
To list all databases by querying the pg_database system catalog using the SELECT statement.
Syntax:
SELECT column_name
FROM pg_database;Example:
Execute the following query to retrieve the names of all databases available on the PostgreSQL server:
SELECT datname
FROM pg_database;Note:
datnameis the column inpg_databasethat stores database names.
Output:
