PostgreSQL.get_schema_info

PostgreSQL.get_schema_info(names_only=True, include_all=False, column_names=None, verbose=False)[source]

Get the names of existing schemas.

Parameters:
  • names_only (bool) – whether to return only the names of the schema names, defaults to True

  • include_all (bool) – whether to list all the available schemas, defaults to False

  • column_names (None | list) – column names of the returned dataframe if names_only=False; when column_names=None, it defaults to ['schema_name', 'schema_id', 'role']

  • verbose (bool | int) – whether to print relevant information in console, defaults to False

Returns:

schema names

Return type:

list | pandas.DataFrame | None

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.

>>> testdb.get_schema_info()
['public']
>>> testdb.get_schema_info(names_only=False, include_all=True)
          schema_name       schema_owner    oid  nspowner
0              public  pg_database_owner   2200      6171
1            pg_toast           postgres     99        10
2          pg_catalog           postgres     11        10
3  information_schema           postgres  13183        10

>>> testdb.create_schema(schema_name='test_schema', verbose=True)
Creating a schema: "test_schema" ... Done.
>>> testdb.get_schema_info()
['public', 'test_schema']

>>> testdb.drop_schema(schema_names=['public', 'test_schema'], verbose=True)
To drop the following schemas from postgres:***@localhost:5432/testdb:
    "public"
    "test_schema"
? [No]|Yes: yes
Dropping ...
    "public" ... Done.
    "test_schema" ... Done.
>>> testdb.get_schema_info() is None
True

>>> testdb.get_schema_info(verbose=True)
No schema exists in the currently-connected database "testdb".

>>> testdb.drop_database(verbose=True)  # Delete the database "testdb"
To drop the database "testdb" from postgres:***@localhost:5432
? [No]|Yes: yes
Dropping "testdb" ... Done.