Thursday, October 10, 2024
Google search engine
HomeData Modelling & AIHow to List All Databases in PostgreSQL

How to List All Databases in PostgreSQL

Introduction

One of the important tasks when managing PostgreSQL servers is listing the existing databases and their tables. There are three ways to list all databases:

  • Using meta-commands
  • Running a query against a server
  • Via the GUI tool pgAdmin.

This tutorial will show you how to list all databases in PostgreSQL and inspect which tables a database contains.

Learn how to list all databases in PostgreSQL.Learn how to list all databases in PostgreSQL.

Prerequisites:

  • PostgreSQL installed and set up
  • Administrator privileges

List Databases via psql Terminal

The psql terminal is a front end to PostgreSQL, allowing users to interact with the server by running queries, issuing them to PostgreSQL, and displaying the results.

psql allows users to use meta-commands, useful commands starting with a backslash \. Use these commands to perform routine tasks, such as to connect to a database, see all databases, etc.

Note: Read our tutorial and learn to create databases in PostgreSQL.

To list all the databases in the server via the psql terminal, follow these steps:

Step 1: Open the SQL Shell (psql) app.

Open the SQL Shell (psql) app.Open the SQL Shell (psql) app.

Step 2: Press ENTER four times to connect to the DB server. Enter your password if asked. If you didn’t set up a password, press ENTER again to connect.

Connect to the database server using psql terminal.Connect to the database server using psql terminal.

Step 3: Run the following command:

\l
Output showing a list of all databases in PostgreSQLOutput showing a list of all databases in PostgreSQL

The output shows a list of all databases currently on the server, including the database name, the owner, encoding, collation, ctype, and access privileges.

Note: If you want to see additional information about size, tablespace, and database descriptions in the output, use \l+.

List Databases via SQL Query

Another method to list databases in PostgreSQL is to query database names from the pg_database catalog via the SELECT statement. Follow these steps:

Step 1: Log in to the server using the SQL Shell (psql) app.

Step 2: Run the following query:

SELECT datname FROM pg_database;
List all databases in psql using the SELECT statement.List all databases in psql using the SELECT statement.

psql runs the query against the server and displays a list of existing databases in the output.

Note: Learn the difference between PostgreSQL and MySQL in our comparison article.

List Databases via pgAdmin

The third method to see databases on the server is to use pgAdmin. pgAdmin is the leading open-source GUI tool for managing PostgreSQL databases.

Follow these steps to see all databases on the server using pgAdmin:

Step 1: Open the pgAdmin app and enter your password to connect to the database server.

Open pgAdmin and connect to the database server.Open pgAdmin and connect to the database server.

Step 2: Expand the Servers tree and then the Databases tree. The tree expands to show a list of all databases on the server. Click the Properties tab to see more information about each database.

Click the Properties tab to see more information about each databaseClick the Properties tab to see more information about each database

List Tables

After listing all existing databases on the server, you can view the tables a database contains. You can achieve this by using psql or using pgAdmin.

See tables in psql

Step 1: While you’re logged in, connect to the database you want to inspect. The syntax is:

\c [database_name]

For example:

An example of connecting to a database in psqlAn example of connecting to a database in psql

Step 2: List all database tables by running:

\dt
The output of table names and their schema, type, and ownerThe output of table names and their schema, type, and owner

The output includes table names and their schema, type, and owner.

If there are no tables in a database, the output states that no relations were found.

Note: To see more information about tables, including their sizes and descriptions, run \dt+.

See tables in pgAdmin:

Step 1: After logging in to pgAdmin, expand the Servers tree, Databases tree, and click the database you want to inspect.

Step 2: In the expanded database tree, click Schemas, followed by Tables. The Properties tab shows a list of all tables, and they show up in the Tables tree as well.

See all tables of a database in pgAdmin.See all tables of a database in pgAdmin.

Conclusion

The guide provided the instructions for listing all databases and their tables on your PostgreSQL server. Choose pgAdmin for a GUI approach or use psql if you prefer to administer your database via a terminal.

Fore more tutorials about PostgreSQL, make sure to read our article and find out how to drop a PostgreSQL database.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments