PostgreSQL.database_exists

PostgreSQL.database_exists(database_name=None)[source]

Checks if a specified database exists.

Parameters:

database_name (str | None) – Name of the database to check; defaults to None.

Returns:

True if the database exists, otherwise False.

Return type:

bool

Examples:

>>> from pyhelpers.dbms import PostgreSQL
>>> testdb = PostgreSQL(database_name='testdb', verbose=True)
Password (postgres@localhost:5432): ***
Creating a database: "testdb" ... Done.
Connecting postgres:***@localhost:5432/testdb ... Successfully.
>>> # Check whether the database "testdb" exists now
>>> testdb.database_exists(database_name='testdb')  # testdb.database_exists()
True
>>> testdb.database_name
'testdb'
>>> # Delete the database "testdb"
>>> testdb.drop_database(verbose=True)
To drop the database "testdb" from postgres:***@localhost:5432
? [No]|Yes: yes
Dropping "testdb" ... Done.
>>> # Check again whether the database "testdb" still exists now
>>> testdb.database_exists(database_name='testdb')
False
>>> testdb.database_name
'postgres'