PostgreSQL.database_exists

PostgreSQL.database_exists(database_name=None)

Check whether a database exists.

Parameters

database_name (str or None) – name of a database, defaults to None

Returns

whether the database exists

Return type

bool

Examples:

>>> from pyhelpers.dbms import PostgreSQL

>>> testdb = PostgreSQL('localhost', 5432, 'postgres', database_name='testdb')
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'