PostgreSQL.get_schema_info

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

Retrieve information about existing schemas.

Parameters:
  • names_only (bool) – Whether to return only the names of the schemas; defaults to True.

  • include_all (bool) – Whether to list all available schemas; defaults to False.

  • column_names (list | None) – Column names for the returned dataframe if names_only=False; defaults to ['schema_name', 'schema_id', 'role'] if column_names=None.

  • verbose (bool | int) – Whether to print relevant information to the console; defaults to False.

Returns:

Names of schemas or a dataframe with schema information if requested.

Return type:

list | pandas.DataFrame | None

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.
>>> 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.