EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Show Databases
 

PostgreSQL Show Databases

Updated May 24, 2023

PostgreSQL Show Databases

 

 

Introduction to PostgreSQL Show Databases

The PostgreSQL provides a show database command. The PostgreSQL Show Databases command is used to see how many databases are present on the server, and this command works on the command line tool psql as well as the query tool in pgadmin4, this is the most common task of the administrator. PostgreSQL, in which single progres(by default database) can store multiple databases, and each database stores a set of files. The PostgreSQL provides different kinds of features to users, like creating a database, drop database and show database, etc.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Psql provides the facility to connect the server and execute a different query against the server. MySQL uses the show databases command to show database, but the show database statement does not work directly in PostgreSQL, it provides some different meta-commands to show database and different query statement.

Syntax of PostgreSQL Show Databases

Given below is the syntax:

\l or \list

Explanation:

  • In the above syntax, where \l is used to list all database lists from the server and the same database list, we can get by using another syntax \list.
  • The function of both syntaxes is the same. It is a Meta command we directly perform on the terminal.
select datname from pg_database;

Explanation:

  • The catalog stores all databases in the above syntax, where datname is used to show available databases from pg_database by default.

How does Show Databases Statement work in PostgreSQL?

Before going to see how to show database statement works, we need some prerequisites as follows:

  • First, you must install PostgreSQL on your system.
  • Use the below command to check whether PostgreSQL is working correctly or not. If the status is active, that means PostgreSQL is installed successfully.

Code:

service postgresql status
  • Also, install the psql command-line PostgreSQL interface.
  • Multiple databases are available to users but can be accessed when a connection is established between client and server.
  • Users perform different operations on databases with the help of show database commands by using like operators.

Show Database Statement as follows:

Code:

\l

This is Meta Command. It is used for the listing of the database for the PostgreSQL database.

Output:

PostgreSQL Show Databases 1

The above screen snapshot, it lists all databases that are present on the server.

Code:

\list

This is also Meta Command, the function of \List is the same as \l.

Output:

PostgreSQL Show Databases 2

We create a database by using the following statement.

Code:

create  database  test;

Output:

PostgreSQL Show Databases 3

Then use \list Meta Command to see the database.

Code:

\list

Output:

PostgreSQL Show Databases 4

We can also show database by using the PostgreSQL query:

Example #1

First, we create a database with a name demo.

Code:

create  database   demo;

Output:

PostgreSQL Show Databases 5

After we perform the show database query as follows:

Code:

select    datname     from    pg_database;

Output:

PostgreSQL Show Databases 6

The created database demo shows the fifth number in the above snapshot.

Another example of a database is before creating a new database, we need to see how many databases may be available on the server then, so we use the following query.

Code:

select    datname    from    pg_database;

Output:

PostgreSQL Show Databases 7

The above snapshot shows 3 by default databases like, Postgres, template1, template, and college is, a user-created database.

Example #2

PostgreSQl provides the facility to create a database with the owner.

Code:

create  database  demo1  with  owner =pg_monitor;

In the above statement, we use a create a statement to create a database, and we created a demo1 database with owner pg_monitor.

Output:

PostgreSQL Show Databases 8

Now see the database column demo1 is created.

Code:

select   datname  from   pg_database;

Output:

column demo1 is created

Example #3

In PostgreSQL, by default database is Postgres, but different users created several databases, and we need only to see those names start with d characters. PostgreSQL provides such a facility.

Code:

Select   datname      from    pg_database
where      datname     like 'd%';

Explanation:

  • In the above statement, we find all databases whose names start with d using the like operator.

Output:

PostgreSQL Show Databases 10

Let’s see another situation: suppose we need a database whose name ends with e.

Code:

select      datname       from   pg_database
where      datname     like '%e';

In the above statement where datname is the database name, pg_database by default database of PostgreSQL, here we use the like operator to show the database.

Output:

ends with e

Example #4

Suppose in our server number of databases is present, and we don’t want to see those database names start with t.

Code:

select   datname     from      pg_database
where   datname   not like 't%';

Explanation:

  • In the above statement, we use not like operators to get the desired result.

Output:

Before the Execution of the above statement, the result is shown in the snapshot.

Before the execution

After the Execution of the above statement, the result is shown in the snapshot.

After Execution

The above snapshot discards the database name that starts with a t character.

Conclusion

From the above article, we also saw the show database Meta command as we implemented the show database using query statements. In a query statement, we use a like operator to perform a smooth search on the database means that as per our requirement, we perform the show database. Finally, it is a time-saving process in PostgreSQL.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Show Databases” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL MAX
  2. PostgreSQL RAISE EXCEPTION
  3. Logical Replication in PostgreSQL
  4. PostgreSQL Formatter
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW