Before creating a database, it’s important to verify that the name you want to use doesn’t already exist. You don’t want to create a database and then get a “database already exists” error.
Listing all of your databases can help you determine whether to move forward with your preferred name or choose a different one. This is just one of the many reasons why you’d want to list your databases.
Whatever reason you may have, this guide will explore six methods you can use to list all of your databases in PostgreSQL.
As with most processes, there are several requirements you must meet:
This method leverages the "\l" meta-command.
If you want additional information on the description, tablespace, or size, you can use the "\l+" command.
This method leverages the "SELECT" statement to query database names from "pg_database." It allows you to specify which databases to retrieve and which ones to leave out.
From the PostgreSQL environment, you can use the below "SELECT" statement to list all databases:
SELECT datname FROM pg_database WHERE datistemplate = false;
This query excludes template databases (datistemplate = false) used for internal purposes but lists user-created databases. It does not exclude other databases that may have configurations similar to templates but are not flagged as "datistemplate." Therefore, if you have custom databases that mimic templates but don't have the "datistemplate" attribute set to "true," they will still appear in the result.
Once you log in to your PostgreSQL server on pgAdmin, here’s how to go about this method:
If you want advanced features beyond just listing databases, you can leverage third-party tools like DBeaver and DataGrip. These tools provide features such as advanced data visualization, cross-platform compatibility, data analysis, and entity relationship (ER) diagrams.
Here’s a step-by-step guide for listing all databases in PostgreSQL using DBeaver:
Scripts in Bash or Python let you programmatically list databases and incorporate this functionality into larger workflows or scheduled tasks.
For our example, we’ll focus on a sample Python script that uses the "psycopg2" library:
import psycopg2
# Establish connection to the PostgreSQL server
conn = psycopg2.connect(
dbname='postgres',
user='yourusername',
password='yourpassword',
host='localhost'
)
cursor = conn.cursor()
# Execute SQL query to list databases
cursor.execute("SELECT datname FROM pg_database WHERE datistemplate = false;")
# Print each database name
for db in cursor.fetchall():
print(db[0])
# Close the connection
cursor.close()
conn.close()
The script above will retrieve all non-template databases from "pg_database."
The connection above assumes that the database name is "postgres." You should replace "postgres" with the name of an accessible database.
If you work with containerized environments in which PostgreSQL runs inside a Docker container, you can still list all of your databases.
First, run psql using the following command:
docker exec -it
psql -U
Next, use the "\l" command to list all databases, just like in a standard PostgreSQL setup.
You may experience several issues when trying to list databases in PostgreSQL. For instance, if you receive a “permission denied” error, this means you don’t have the required privileges to perform that activity.
To remedy this, you can edit the "pg_hba.conf" file, which controls the client authentication settings. However, this file is critical for database security. Changing it without careful consideration could expose your database to unauthorized access. Resort to editing it only if your basic role and privilege management doesn't resolve the issue, and ensure you follow security best practices.
Ensure you’ve correctly configured the authentication method (such as md5, scram-sha-256, or trust) for your network or IP range. Then, restart PostgreSQL to apply any changes.
If you experience connection issues, there are several things you can do, including:
The best method for listing your databases will depend on your preferences and experience.
With Delinea, you can simplify your database management even more. This solution gives you:
Sign up for a free trial today to see Delinea in action.