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.
- 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.
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.
Step 3: Run the following command:
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
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;
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.
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.
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:
Note: If you are using Linux, see how to connect to a PostgreSQL database from a Linux command line.
Step 2: List all database tables by running:
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
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.
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.