PostgreSQL Show Databases
Summary: in this tutorial, you will learn how to show databases in a PostgreSQL database server.
In MySQL, you can show all databases in the server using SHOW DATABASES
statement.
PostgreSQL does not directly support the SHOW DATABASES
statement but offers you something similar. PostgreSQL provides you with two ways to show databases in a PostgreSQL database server.
Listing databases in PostgreSQL using psql command
First, open the Command Prompt on Windows or Terminal on Unix-like systems and connect to the PostgreSQL server:
This statement uses the postgres user to connect to the local PostgreSQL server. It’ll prompt you to enter a password.
Second, show all the databases in the current server using the \l
command:
It’ll show the following output:
To display more information on databases, you can use the \l+
command:
Listing databases in PostgreSQL using SELECT statement
The following statement retrieves the database names from the pg_database
view:
Output:
The query returns four databases in the current PostgreSQL server.
Summary
- Use
\l
or\l+
inpsql
to show all databases in a PostgreSQL database server. - Use the
SELECT
statement to query data from thepg_database
to retrieve all the database names in a PostgreSQL database server.